def plot_convolution(): response = thinkdsp.read_wave('180961__kleeb__gunshots.wav') response = response.segment(start=0.26, duration=5.0) response.normalize() dt = 1 shift = dt * response.framerate factor = 0.5 gun2 = response + shifted_scaled(response, shift, factor) gun2.plot() thinkplot.config(xlabel='time (s)', ylabel='amplitude', ylim=[-1.05, 1.05], legend=False) thinkplot.save(root='systems8') signal = thinkdsp.SawtoothSignal(freq=410) wave = signal.make_wave(duration=0.1, framerate=response.framerate) total = 0 for j, y in enumerate(wave.ys): total += shifted_scaled(response, j, y) total.normalize() wave.make_spectrum().plot(high=500, color='0.7', label='original') segment = total.segment(duration=0.2) segment.make_spectrum().plot(high=1000, label='convolved') thinkplot.config(xlabel='frequency (Hz)', ylabel='amplitude') thinkplot.save(root='systems9')
def view_harmonics(freq, framerate): signal = thinkdsp.SawtoothSignal(freq) wave = signal.make_wave(duration=0.5, framerate=framerate) spectrum = wave.make_spectrum() spectrum.plot(color='blue') thinkplot.show(xlabel='frequency', ylabel='amplitude') display(wave.make_audio())
def get_sawtooth_wave_data(freq, offset, duration=0.1, framerate=40000, noise_ratio=0): signal = thinkdsp.SawtoothSignal(freq=freq, amp=1.0, offset=offset) 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 get_sawtooth_square_wave(freq, offset, duration=0.1, framerate=40000): signal_sawtooth = thinkdsp.SawtoothSignal(freq=freq, amp=1.0, offset=offset) signal_square = thinkdsp.SquareSignal(freq=freq, amp=1.0, offset=offset + 2 * numpy.pi * 0.2) components = [signal_sawtooth, signal_square] signal = thinkdsp.SumSignal(*components) wave = signal.make_wave(duration=duration, start=0, framerate=40000) wave.ys = wave.ys / len(components) # normalization return wave.ys
def plot_sawtooth(response): signal = thinkdsp.SawtoothSignal(freq=441) wave = signal.make_wave(duration=0.1, framerate=response.framerate) total = 0 for t, y in zip(wave.ts, wave.ys): total += shifted_scaled(response, t, y) total.normalize() high = 5000 wave.make_spectrum().plot(high=high, color='0.7', label='original') segment = total.segment(duration=0.2) segment.make_spectrum().plot(high=high, label='convolved') thinkplot.config(xlabel='Frequency (Hz)', ylabel='Amplitude') thinkplot.save(root='systems9')
def make_figures(): """Makes figures showing complex signals. """ amps = numpy.array([0.6, 0.25, 0.1, 0.05]) freqs = [100, 200, 300, 400] framerate = 11025 ts = numpy.linspace(0, 1, framerate) ys = synthesize1(amps, freqs, ts) print(ys) thinkplot.preplot(2) n = framerate / 25 thinkplot.plot(ts[:n], ys[:n].real, label='real') thinkplot.plot(ts[:n], ys[:n].imag, label='imag') thinkplot.save(root='dft1', xlabel='time (s)', ylabel='wave', ylim=[-1.05, 1.05]) ys = synthesize2(amps, freqs, ts) amps2 = amps * numpy.exp(1.5j) ys2 = synthesize2(amps2, freqs, ts) thinkplot.preplot(2) thinkplot.plot(ts[:n], ys.real[:n], label=r'$\phi_0 = 0$') thinkplot.plot(ts[:n], ys2.real[:n], label=r'$\phi_0 = 1.5$') thinkplot.save(root='dft2', xlabel='time (s)', ylabel='wave', ylim=[-1.05, 1.05], loc='lower right') framerate = 10000 signal = thinkdsp.SawtoothSignal(freq=500) wave = signal.make_wave(duration=0.1, framerate=framerate) hs = dft(wave.ys) amps = numpy.absolute(hs) N = len(hs) fs = numpy.arange(N) * framerate / N thinkplot.plot(fs, amps) thinkplot.save(root='dft3', xlabel='frequency (Hz)', ylabel='amplitude', legend=False)
def main(): names = ['date', 'open', 'high', 'low', 'close', 'volume'] df = pd.read_csv('fb.csv', header=0, names=names, parse_dates=[0]) ys = df.close.values[::-1] close = thinkdsp.Wave(ys, framerate=1) plot_wave_and_spectrum(close, root='diff_int1') change = thinkdsp.Wave(np.diff(ys), framerate=1) plot_wave_and_spectrum(change, root='diff_int2') plot_filters(close) plot_diff_deriv(close) signal = thinkdsp.SawtoothSignal(freq=50) in_wave = signal.make_wave(duration=0.1, framerate=44100) plot_sawtooth_and_spectrum(in_wave, 'diff_int6') out_wave = in_wave.cumsum() out_wave.unbias() plot_sawtooth_and_spectrum(out_wave, 'diff_int7') plot_integral(close) plot_ratios(in_wave, out_wave)
gaussian = scipy.signal.gaussian(M=11, std=2) gaussian /= sum(gaussian) # In[ ]: names = ['date', 'open', 'high', 'low', 'close', 'volume'] df = pd.read_csv('C:\Users\esehu\Downloads\FB.csv', header=0, names=names, parse_dates=[0]) df.head() # In[ ]: signal = thinkdsp.SawtoothSignal(freq=440) wave = signal.make_wave(duration=1.0, framerate=44100) wave.make_audio() # In[ ]: window = np.ones(11) window /= sum(window) thinkplot.plot(window) # In[ ]: segment = wave.segment(duration=0.01) segment.plot() thinkplot.config(xlabel='Time (s)', ylim=[-1.05, 1.05])
plt.rcParams['font.sans-serif'] = ['SimHei'] #图表上可以显示中文 plt.rcParams['axes.unicode_minus'] = False #图表上可以显示负号 signal = thinkdsp.TriangleSignal(200) #200HZ的三角波 wave = signal.make_wave(duration=0.5, framerate=10000) #长度0.5,每秒10000次 period = signal.period segment = wave.segment(start=0, duration=period * 3) plt.subplot(2, 2, 1) plt.title('200HZ三角波') segment.plot() spectrum = wave.make_spectrum() plt.subplot(2, 2, 2) plt.title('三角波频谱') spectrum.plot() #锯齿波 signal1 = thinkdsp.SawtoothSignal(200) wave = signal1.make_wave(duration=0.5, framerate=10000) period = signal1.period segment = wave.segment(start=0, duration=period * 3) plt.subplot(2, 2, 3) plt.title('200HZ锯齿波') segment.plot() spectrum = wave.make_spectrum() plt.subplot(2, 2, 4) plt.title('锯齿波频谱') spectrum.plot() plt.show()
# #close.plot() #pyplot.show() # #cumsum_wav.plot() #pyplot.show() # #cumsum_spec.plot() #pyplot.show() #these operations only work on periodic waves... #inv_close = int_spec.make_wave() #inv_close.plot() #pyplot.show() sq_sig = thinkdsp.SawtoothSignal() sq_wav = sq_sig.make_wave() sq_wav.plot() pyplot.xlim(0, .01) #pyplot.show() cs_wav = sq_wav.cumsum() cs_wav.normalize() cs_wav.unbias() cs_wav.plot() pyplot.xlim(0, .01) #pyplot.show() sq_spec = sq_wav.make_spectrum() int_spec = sq_spec.integrate()
signal = thinkdsp.CosSignal(4500) duration = signal.period * 5 segment = signal.make_wave(duration, framerate=framerate) segment.plot() # In[6]: framerate = 10000 signal = thinkdsp.CosSignal(5500) duration = signal.period * 5 segment = signal.make_wave(duration, framerate=framerate) segment.plot() # In[7]: signal = thinkdsp.SawtoothSignal(500) wave = signal.make_wave(duration=1, framerate=10000) segment = wave.segment(duration=0.005) segment.plot() # In[8]: hs = np.fft.rfft(wave.ys) hs # In[9]: n = len(wave.ys) d = 1 / wave.framerate fs = np.fft.rfftfreq(n, d) fs
signal_tri.plot() wave = signal_tri.make_wave(duration=0.01, framerate=10000) spectrum = wave.make_spectrum() plt.subplot(334) spectrum.plot() s = af(spectrum) plt.subplot(337) s.plot() signal_squ = thinkdsp.SquareSignal(200) #生成一个200Hz的三角波 plt.subplot(332) signal_squ.plot() wave = signal_squ.make_wave(duration=0.01, framerate=10000) spectrum = wave.make_spectrum() plt.subplot(335) spectrum.plot() s = af(spectrum) plt.subplot(338) s.plot() signal_saw = thinkdsp.SawtoothSignal(200) #生成一个200Hz的锯齿波 plt.subplot(333) signal_saw.plot() wave = signal_saw.make_wave(duration=0.01, framerate=10000) spectrum = wave.make_spectrum() plt.subplot(336) spectrum.plot() s = af(spectrum) plt.subplot(339) s.plot() plt.show()