def chirp_spectrum(): """Plots the spectrum of a one-second one-octave linear chirp. """ signal = thinkdsp.Chirp(start=220, end=440) wave = signal.make_wave(duration=1) thinkplot.preplot(3, cols=3) duration = 0.01 wave.segment(0, duration).plot() thinkplot.config(ylim=[-1.05, 1.05]) thinkplot.subplot(2) wave.segment(0.5, duration).plot() thinkplot.config(yticklabels='invisible', xlabel='Time (s)') thinkplot.subplot(3) wave.segment(0.9, duration).plot() thinkplot.config(yticklabels='invisible') thinkplot.save('chirp3') spectrum = wave.make_spectrum() spectrum.plot(high=700) thinkplot.save('chirp1', xlabel='Frequency (Hz)')
def chirp_spectrum(): """Plots the spectrum of a one-second one-octave linear chirp. """ signal = thinkdsp.Chirp(start=220, end=440) wave = signal.make_wave(duration=1) spectrum = wave.make_spectrum() spectrum.plot(high=660) thinkplot.save('chirp1', xlabel='frequency (Hz)', ylabel='amplitude')
def get_chirp_wave_data(freq, offset, duration=0.1, framerate=40000, noise_ratio=0): signal = thinkdsp.Chirp(start=freq * 0.5, end=1.5, amp=1.0) wave = signal.make_wave(duration=duration, start=0, framerate=framerate) if noise_ratio != 0: wave.ys += numpy.random.standard_normal(wave.ys.shape) * noise_ratio return wave.ys
def invert_spectrogram(): """Tests Spectrogram.make_wave. """ signal = thinkdsp.Chirp(start=220, end=440) wave = signal.make_wave(duration=1, framerate=11025) spectrogram = wave.make_spectrogram(seg_length=512) wave2 = spectrogram.make_wave() for i, (y1, y2) in enumerate(zip(wave.ys, wave2.ys)): if abs(y1 - y2) > 1e-14: print(i, y1, y2)
def eye_of_sauron(start, end): """Plots the spectrum of a chirp. start: initial frequency end: final frequency """ signal = thinkdsp.Chirp(start=start, end=end) wave = signal.make_wave(duration=0.5) spectrum = wave.make_spectrum() spectrum.plot(high=1200) thinkplot.config(xlabel='frequency (Hz)', ylabel='amplitude')
def chirp_spectrogram(): """Makes a spectrogram of a one-second one-octave linear chirp. """ signal = thinkdsp.Chirp(start=220, end=440) wave = signal.make_wave(duration=1, framerate=11025) spectrogram = wave.make_spectrogram(seg_length=512) print('time res', spectrogram.time_res) print('freq res', spectrogram.freq_res) print('product', spectrogram.time_res * spectrogram.freq_res) spectrogram.plot(high=32) thinkplot.save('chirp2', xlabel='time (s)', ylabel='frequency (Hz)')
def main(): chirp_spectrogram() chirp_spectrum() return violin_spectrogram() return invert_image() return invert_spectrogram() return overlapping_windows() return window_plot() three_spectrums() return linear_chirp_evaluate(range(4)) signal = thinkdsp.Chirp(start=220, end=880) wave1 = signal.make_wave(duration=2) wave1.apodize() signal = thinkdsp.ExpoChirp(start=220, end=880) wave2 = signal.make_wave(duration=2) wave2.apodize() filename = 'chirp.wav' wfile = thinkdsp.WavFileWriter(filename, wave1.framerate) wfile.write(wave1) wfile.write(wave2) wfile.close
def testChirp(self): signal = thinkdsp.Chirp(100, 200, 0.5) result = signal.evaluate([1.001, 1.002, 1.005]) self.assertAlmostEqual(result[0], 0.5) self.assertAlmostEqual(result[2], -0.49384417)
# In[1]: from __future__ import print_function, division get_ipython().magic(u'matplotlib inline') import thinkdsp import thinkplot import numpy as np from ipywidgets import interact, interactive, fixed import ipywidgets as widgets # In[2]: signal = thinkdsp.Chirp(start=220, end=880) wave1 = signal.make_wave(duration=2) wave1.make_audio() # In[3]: wave1.segment(start=0, duration=0.01).plot() # In[4]: wave1.segment(start=0.9, duration=0.01).plot() # In[5]: def evaluate(self, ts):
def testChirp(self): signal = thinkdsp.Chirp(100, 200, 0.5) result = signal.evaluate([0, 0.001, 0.002]) self.assertAlmostEqual(result[0], 0.5) self.assertAlmostEqual(result[2], -0.29389263)