def triangle_example(freq): """Makes a figure showing a triangle wave. freq: frequency in Hz """ framerate = 10000 signal = thinkdsp.TriangleSignal(freq) duration = signal.period*3 segment = signal.make_wave(duration, framerate=framerate) segment.plot() thinkplot.save(root='triangle-%d-1' % freq, xlabel='Time (s)', axis=[0, duration, -1.05, 1.05]) wave = signal.make_wave(duration=0.5, framerate=framerate) spectrum = wave.make_spectrum() thinkplot.preplot(cols=2) spectrum.plot() thinkplot.config(xlabel='Frequency (Hz)', ylabel='Amplitude') thinkplot.subplot(2) spectrum.plot() thinkplot.config(ylim=[0, 500], xlabel='Frequency (Hz)') thinkplot.save(root='triangle-%d-2' % freq)
def aliasing_example(offset=0.000003): """Makes a figure showing the effect of aliasing. """ framerate = 10000 def plot_segment(freq): signal = thinkdsp.CosSignal(freq) duration = signal.period*4 thinkplot.Hlines(0, 0, duration, color='gray') segment = signal.make_wave(duration, framerate=framerate*10) segment.plot(linewidth=0.5, color='gray') segment = signal.make_wave(duration, framerate=framerate) segment.plot_vlines(label=freq, linewidth=4) thinkplot.preplot(rows=2) plot_segment(4500) thinkplot.config(axis=[-0.00002, 0.0007, -1.05, 1.05]) thinkplot.subplot(2) plot_segment(5500) thinkplot.config(axis=[-0.00002, 0.0007, -1.05, 1.05]) thinkplot.save(root='aliasing1', xlabel='Time (s)', formats=FORMATS)
def plot_facebook(): """Plot Facebook prices and a smoothed time series. """ names = ['date', 'open', 'high', 'low', 'close', 'volume'] df = pd.read_csv('fb.csv', header=0, names=names, parse_dates=[0]) close = df.close.values[::-1] dates = df.date.values[::-1] days = (dates - dates[0]) / np.timedelta64(1,'D') M = 30 window = np.ones(M) window /= sum(window) smoothed = np.convolve(close, window, mode='valid') smoothed_days = days[M//2: len(smoothed) + M//2] thinkplot.plot(days, close, color=GRAY, label='daily close') thinkplot.plot(smoothed_days, smoothed, label='30 day average') last = days[-1] thinkplot.config(xlabel='Time (days)', ylabel='Price ($)', xlim=[-7, last+7], legend=True, loc='lower right') thinkplot.save(root='convolution1')
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 plot_sincs(): start = 1.0 duration = 0.01 factor = 4 short = wave.segment(start=start, duration=duration) short.plot() sampled = sample(short, factor) sampled.plot_vlines(color='gray') spectrum = sampled.make_spectrum() boxcar = make_boxcar(spectrum, factor) sinc = boxcar.make_wave() sinc.shift(sampled.ts[0]) sinc.roll(len(sinc)//2) thinkplot.preplot(1) sinc.plot() # CAUTION: don't call plot_sinc_demo with a large wave or it will # fill memory and crash plot_sinc_demo(short, 4) start = short.start + 0.004 duration = 0.00061 plot_sinc_demo(short, 4, start, duration) thinkplot.config(xlim=[start, start+duration], ylim=[-0.05, 0.17], legend=False)
def plot_ratios(in_wave, out_wave): # compare filters for cumsum and integration diff_window = np.array([1.0, -1.0]) padded = thinkdsp.zero_pad(diff_window, len(in_wave)) diff_wave = thinkdsp.Wave(padded, framerate=in_wave.framerate) diff_filter = diff_wave.make_spectrum() cumsum_filter = diff_filter.copy() cumsum_filter.hs = 1 / cumsum_filter.hs cumsum_filter.plot(label='cumsum filter', color=GRAY, linewidth=7) integ_filter = cumsum_filter.copy() integ_filter.hs = integ_filter.framerate / (PI2 * 1j * integ_filter.fs) integ_filter.plot(label='integral filter') thinkplot.config(xlim=[0, integ_filter.max_freq], yscale='log', legend=True) thinkplot.save('diff_int8') # compare cumsum filter to actual ratios cumsum_filter.plot(label='cumsum filter', color=GRAY, linewidth=7) in_spectrum = in_wave.make_spectrum() out_spectrum = out_wave.make_spectrum() ratio_spectrum = out_spectrum.ratio(in_spectrum, thresh=1) ratio_spectrum.plot(label='ratio', style='.', markersize=4) thinkplot.config(xlabel='Frequency (Hz)', ylabel='Amplitude ratio', xlim=[0, integ_filter.max_freq], yscale='log', legend=True) thinkplot.save('diff_int9')
def plot_derivative(wave, wave2): # compute the derivative by spectral decomposition spectrum = wave.make_spectrum() spectrum3 = wave.make_spectrum() spectrum3.differentiate() # plot the derivative computed by diff and differentiate wave3 = spectrum3.make_wave() wave2.plot(color="0.7", label="diff") wave3.plot(label="derivative") thinkplot.config(xlabel="days", xlim=[0, 1650], ylabel="dollars", loc="upper left") thinkplot.save(root="systems4") # plot the amplitude ratio compared to the diff filter amps = spectrum.amps amps3 = spectrum3.amps ratio3 = amps3 / amps thinkplot.preplot(1) thinkplot.plot(ratio3, label="ratio") window = numpy.array([1.0, -1.0]) padded = zero_pad(window, len(wave)) fft_window = numpy.fft.rfft(padded) thinkplot.plot(abs(fft_window), color="0.7", label="filter") thinkplot.config( xlabel="frequency (1/days)", xlim=[0, 1650 / 2], ylabel="amplitude ratio", ylim=[0, 4], loc="upper left" ) thinkplot.save(root="systems5")
def plot_filters(close): """Plots the filter that corresponds to diff, deriv, and integral. """ thinkplot.preplot(3, cols=2) diff_window = np.array([1.0, -1.0]) diff_filter = make_filter(diff_window, close) diff_filter.plot(label='diff') deriv_filter = close.make_spectrum() deriv_filter.hs = PI2 * 1j * deriv_filter.fs deriv_filter.plot(label='derivative') thinkplot.config(xlabel='Frequency (1/day)', ylabel='Amplitude ratio', loc='upper left') thinkplot.subplot(2) integ_filter = deriv_filter.copy() integ_filter.hs = 1 / (PI2 * 1j * integ_filter.fs) integ_filter.plot(label='integral') thinkplot.config(xlabel='Frequency (1/day)', ylabel='Amplitude ratio', yscale='log') thinkplot.save('diff_int3')
def main(): # make_figures() wave = thinkdsp.read_wave('28042__bcjordan__voicedownbew.wav') wave.unbias() wave.normalize() track_pitch(wave) return thinkplot.preplot(rows=1, cols=2) plot_shifted(wave, 0.0003) thinkplot.config(xlabel='time (s)', ylabel='amplitude', ylim=[-1, 1]) thinkplot.subplot(2) plot_shifted(wave, 0.00225) thinkplot.config(xlabel='time (s)', ylim=[-1, 1]) thinkplot.save(root='autocorr3')
def plot_diff_filter(close): """Plots the filter that corresponds to first order finite difference. """ diff_window = np.array([1.0, -1.0]) diff_filter = make_filter(diff_window, close) diff_filter.plot() thinkplot.config(xlabel='Frequency (1/day)', ylabel='Amplitude ratio') thinkplot.save('diff_int3')
def dct_plot(): signal = thinkdsp.TriangleSignal(freq=400) wave = signal.make_wave(duration=1.0, framerate=10000) dct = wave.make_dct() dct.plot() thinkplot.config(xlabel='Frequency (Hz)', ylabel='DCT') thinkplot.save(root='dct1', formats=['pdf', 'eps'])
def plot_convolution(response): """Plots the impulse response and a shifted, scaled copy. """ shift = 1 factor = 0.5 gun2 = response + shifted_scaled(response, shift, factor) gun2.plot() thinkplot.config(xlabel='Time (s)', ylim=[-1.05, 1.05], legend=False) thinkplot.save(root='systems8')
def process_noise(signal, root='white'): """Plots wave and spectrum for noise signals. signal: Signal root: string used to generate file names """ framerate = 11025 wave = signal.make_wave(duration=0.5, framerate=framerate) # 0: waveform segment = wave.segment(duration=0.1) segment.plot(linewidth=1, alpha=0.5) thinkplot.save(root=root+'noise0', xlabel='Time (s)', ylim=[-1.05, 1.05]) spectrum = wave.make_spectrum() # 1: spectrum spectrum.plot_power(linewidth=1, alpha=0.5) thinkplot.save(root=root+'noise1', xlabel='Frequency (Hz)', ylabel='Power', xlim=[0, spectrum.fs[-1]]) slope, _, _, _, _ = spectrum.estimate_slope() print('estimated slope', slope) # 2: integrated spectrum integ = spectrum.make_integrated_spectrum() integ.plot_power() thinkplot.save(root=root+'noise2', xlabel='Frequency (Hz)', ylabel='Cumulative fraction of total power', xlim=[0, framerate/2]) # 3: log-log power spectrum spectrum.hs[0] = 0 thinkplot.preplot(cols=2) spectrum.plot_power(linewidth=1, alpha=0.5) thinkplot.config(xlabel='Frequency (Hz)', ylabel='Power', xlim=[0, framerate/2]) thinkplot.subplot(2) spectrum.plot_power(linewidth=1, alpha=0.5) thinkplot.config(xlabel='Frequency (Hz)', xscale='log', yscale='log', xlim=[0, framerate/2]) thinkplot.save(root=root+'noise3')
def plot_diff_deriv(close): change = thinkdsp.Wave(np.diff(close.ys), framerate=1) deriv_spectrum = close.make_spectrum().differentiate() deriv = deriv_spectrum.make_wave() low, high = 0, 50 thinkplot.preplot(2) thinkplot.plot(change.ys[low:high], label='diff') thinkplot.plot(deriv.ys[low:high], label='derivative') thinkplot.config(xlabel='Time (day)', ylabel='Price change ($)') thinkplot.save('diff_int4')
def plot_sampling(wave, root): ax1 = thinkplot.preplot(2, rows=2) wave.make_spectrum(full=True).plot(label='spectrum') thinkplot.config(xlim=XLIM, xticklabels='invisible') ax2 = thinkplot.subplot(2) sampled = sample(wave, 4) sampled.make_spectrum(full=True).plot(label='sampled') thinkplot.config(xlim=XLIM, xlabel='Frequency (Hz)') thinkplot.save(root=root, formats=FORMATS)
def plot_wave_and_spectrum(wave, root): """Makes a plot showing """ thinkplot.preplot(cols=2) wave.plot() thinkplot.config(xlabel="days", xlim=[0, 1650], ylabel="dollars", legend=False) thinkplot.subplot(2) spectrum = wave.make_spectrum() print(spectrum.estimate_slope()) spectrum.plot() thinkplot.config(xlabel="frequency (1/days)", ylabel="power", xscale="log", yscale="log", legend=False) thinkplot.save(root=root)
def plot_amen2(): wave = thinkdsp.read_wave('263868__kevcio__amen-break-a-160-bpm.wav') wave.normalize() ax1 = thinkplot.preplot(6, rows=4) wave.make_spectrum(full=True).plot(label='spectrum') xlim = [-22050-250, 22050] thinkplot.config(xlim=xlim, xticklabels='invisible') ax2 = thinkplot.subplot(2) impulses = make_impulses(wave, 4) impulses.make_spectrum(full=True).plot(label='impulses') thinkplot.config(xlim=xlim, xticklabels='invisible') ax3 = thinkplot.subplot(3) sampled = wave * impulses spectrum = sampled.make_spectrum(full=True) spectrum.plot(label='sampled') thinkplot.config(xlim=xlim, xticklabels='invisible') ax4 = thinkplot.subplot(4) spectrum.low_pass(5512.5) spectrum.plot(label='filtered') thinkplot.config(xlim=xlim, xlabel='Frequency (Hz)') thinkplot.save(root='sampling4', formats=FORMATS)
def plot_autocorr(): """Plots autocorrelation for pink noise with different parameters """ np.random.seed(19) thinkplot.preplot(3) for beta in [1.7, 1.0, 0.3]: label = r'$\beta$ = %.1f' % beta plot_pink_autocorr(beta, label) thinkplot.config(xlabel='Lag', ylabel='Correlation', xlim=[-5, 1000], ylim=[-0.05, 1.05]) thinkplot.save(root='autocorr4')
def plot_filter(): """Plots the filter that corresponds to a 2-element moving average. """ impulse = np.zeros(8) impulse[0] = 1 wave = thinkdsp.Wave(impulse, framerate=8) impulse_spectrum = wave.make_spectrum(full=True) window_array = np.array([0.5, 0.5, 0, 0, 0, 0, 0, 0,]) window = thinkdsp.Wave(window_array, framerate=8) filtr = window.make_spectrum(full=True) filtr.plot() thinkplot.config(xlabel='Frequency', ylabel='Amplitude') thinkplot.save('systems1')
def plot_autocorr(): """Plots autocorrelation for pink noise with different parameters """ numpy.random.seed(19) thinkplot.preplot(3) for beta in [1.2, 1.0, 0.7]: label = r'$\beta$ = %.1f' % beta plot_pink_autocorr(beta, label) thinkplot.config(xlabel='lag', ylabel='correlation', xlim=[-1, 200], ylim=[-0.05, 1.05]) thinkplot.save(root='autocorr4')
def main(): gss = read_gss() sample = utils.ResampleByYear(gss) grouped_year = sample.groupby(['year']) plot_relig(grouped_year) thinkplot.config(xlabel='Year of survey', ylabel='Percent') thinkplot.show() return # run the resamples dfs = [run(gss) for _ in range(101)] # make the plots single_plot(dfs) multi_plot(dfs)
def plot_sawtooth_and_spectrum(wave, root): """Makes a plot showing a sawtoothwave and its spectrum. """ thinkplot.preplot(cols=2) wave.plot() thinkplot.config(xlabel='Time (s)') thinkplot.subplot(2) spectrum = wave.make_spectrum() spectrum.plot() thinkplot.config(xlabel='Frequency (Hz)', ylabel='Amplitude', xlim=[0, spectrum.fs[-1]]) thinkplot.save(root)
def plot_shifted(wave, offset=0.001, start=0.2): thinkplot.preplot(2) segment1 = wave.segment(start=start, duration=0.01) segment1.plot(linewidth=2, alpha=0.8) # start earlier and then shift times to line up segment2 = wave.segment(start=start - offset, duration=0.01) segment2.shift(offset) segment2.plot(linewidth=2, alpha=0.4) corr = segment1.corr(segment2) text = r'$\rho =$ %.2g' % corr thinkplot.text(segment1.start + 0.0005, -0.8, text) thinkplot.config(xlabel='Time (s)', xlim=[start, start + duration], ylim=[-1, 1])
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 plot_sawtooth_and_spectrum(wave, root): """Makes a plot showing a sawtoothwave and its spectrum. """ thinkplot.preplot(cols=2) wave.plot() thinkplot.config(xlabel='Time (s)') thinkplot.subplot(2) spectrum = wave.make_spectrum() spectrum.plot() thinkplot.config( xlabel='Frequency (Hz)', #ylabel='Amplitude', xlim=[0, spectrum.fs[-1]]) thinkplot.save(root)
def plot_integral(close): deriv_spectrum = close.make_spectrum().differentiate() integ_spectrum = deriv_spectrum.integrate() print(integ_spectrum.hs[0]) integ_spectrum.hs[0] = 0 thinkplot.preplot(2) integ_wave = integ_spectrum.make_wave() close.plot(label='closing prices') integ_wave.plot(label='integrated derivative') thinkplot.config(xlabel='Time (day)', ylabel='Price ($)', legend=True, loc='upper left') thinkplot.save('diff_int5')
def plot_correlate(): """Plots the autocorrelation function computed by np. """ wave = thinkdsp.read_wave('28042__bcjordan__voicedownbew.wav') wave.normalize() segment = wave.segment(start=0.2, duration=0.01) lags, corrs = autocorr(segment) N = len(segment) corrs2 = np.correlate(segment.ys, segment.ys, mode='same') lags = np.arange(-N//2, N//2) thinkplot.plot(lags, corrs2) thinkplot.config(xlabel='Lag', ylabel='Correlation', xlim=[-N//2, N//2]) thinkplot.save(root='autocorr9')
def plot_amen(): wave = thinkdsp.read_wave('263868__kevcio__amen-break-a-160-bpm.wav') wave.normalize() ax1 = thinkplot.preplot(2, rows=2) wave.make_spectrum(full=True).plot() xlim = [-22050, 22050] thinkplot.config(xlim=xlim, xticklabels='invisible') ax2 = thinkplot.subplot(2, sharey=ax1) sampled = sample(wave, 4) sampled.make_spectrum(full=True).plot() thinkplot.config(xlim=xlim, xlabel='Frequency (Hz)') thinkplot.show() return
def plot_correlate(): """Plots the autocorrelation function computed by np. """ wave = thinkdsp.read_wave('28042__bcjordan__voicedownbew.wav') wave.normalize() segment = wave.segment(start=0.2, duration=0.01) lags, corrs = autocorr(segment) N = len(segment) corrs2 = np.correlate(segment.ys, segment.ys, mode='same') lags = np.arange(-N // 2, N // 2) thinkplot.plot(lags, corrs2) thinkplot.config(xlabel='Lag', ylabel='Correlation', xlim=[-N // 2, N // 2]) thinkplot.save(root='autocorr9')
def plot_shifted(wave, shift=0.002, start=0.2): """Plots two segments of a wave with different start times. wave: Wave shift: difference in start time (seconds) start: start time in seconds """ thinkplot.preplot(num=2) segment1 = wave.segment(start=start, duration=0.01) segment1.plot(linewidth=2, alpha=0.8) segment2 = wave.segment(start=start-shift, duration=0.01) segment2.plot(linewidth=2, alpha=0.4) corr = segment1.corr(segment2) text = r'$\rho =$ %.2g' % corr thinkplot.text(0.0005, -0.8, text) thinkplot.config(xlabel='time (s)', ylim=[-1, 1])
def plot_shifted(wave, shift=0.002, start=0.2): """Plots two segments of a wave with different start times. wave: Wave shift: difference in start time (seconds) start: start time in seconds """ thinkplot.preplot(num=2) segment1 = wave.segment(start=start, duration=0.01) segment1.plot(linewidth=2, alpha=0.8) segment2 = wave.segment(start=start - shift, duration=0.01) segment2.plot(linewidth=2, alpha=0.4) corr = segment1.corr(segment2) text = r'$\rho =$ %.2g' % corr thinkplot.text(0.0005, -0.8, text) thinkplot.config(xlabel='time (s)', ylim=[-1, 1])
def plot_gaussian(): """Makes a plot showing the effect of convolution with a boxcar window. """ # start with a square signal signal = thinkdsp.SquareSignal(freq=440) wave = signal.make_wave(duration=1, framerate=44100) spectrum = wave.make_spectrum() # and a boxcar window boxcar = numpy.ones(11) boxcar /= sum(boxcar) # and a gaussian window gaussian = scipy.signal.gaussian(M=11, std=2) gaussian /= sum(gaussian) thinkplot.preplot(2) thinkplot.plot(boxcar, label='boxcar') thinkplot.plot(gaussian, label='Gaussian') thinkplot.config(xlabel='index', ylabel='amplitude') thinkplot.save(root='convolution7') ys = numpy.convolve(wave.ys, gaussian, mode='same') smooth = thinkdsp.Wave(ys, framerate=wave.framerate) spectrum2 = smooth.make_spectrum() # plot the ratio of the original and smoothed spectrum amps = spectrum.amps amps2 = spectrum2.amps ratio = amps2 / amps ratio[amps < 560] = 0 # plot the same ratio along with the FFT of the window padded = zero_pad(gaussian, len(wave)) dft_gaussian = numpy.fft.rfft(padded) thinkplot.plot(abs(dft_gaussian), color='0.7', label='Gaussian filter') thinkplot.plot(ratio, label='amplitude ratio') thinkplot.config(xlabel='frequency (Hz)', ylabel='amplitude ratio', xlim=[0, 22050], legend=False) thinkplot.save(root='convolution8')
def plot_gaussian(): """Makes a plot showing the effect of convolution with a boxcar window. """ # start with a square signal signal = thinkdsp.SquareSignal(freq=440) wave = signal.make_wave(duration=1, framerate=44100) spectrum = wave.make_spectrum() # and a boxcar window boxcar = np.ones(11) boxcar /= sum(boxcar) # and a gaussian window gaussian = scipy.signal.gaussian(M=11, std=2) gaussian /= sum(gaussian) thinkplot.preplot(2) thinkplot.plot(boxcar, label='boxcar') thinkplot.plot(gaussian, label='Gaussian') thinkplot.config(xlabel='Index', legend=True) thinkplot.save(root='convolution7') ys = np.convolve(wave.ys, gaussian, mode='same') smooth = thinkdsp.Wave(ys, framerate=wave.framerate) spectrum2 = smooth.make_spectrum() # plot the ratio of the original and smoothed spectrum amps = spectrum.amps amps2 = spectrum2.amps ratio = amps2 / amps ratio[amps<560] = 0 # plot the same ratio along with the FFT of the window padded = thinkdsp.zero_pad(gaussian, len(wave)) dft_gaussian = np.fft.rfft(padded) thinkplot.plot(abs(dft_gaussian), color=GRAY, label='Gaussian filter') thinkplot.plot(ratio, label='amplitude ratio') thinkplot.config(xlabel='Frequency (Hz)', ylabel='Amplitude ratio', xlim=[0, 22050]) thinkplot.save(root='convolution8')
def plot_serial_corr(): """Makes a plot showing serial correlation for pink noise. """ numpy.random.seed(19) betas = numpy.linspace(0, 2, 21) corrs = [] for beta in betas: signal = thinkdsp.PinkNoise(beta=beta) wave = signal.make_wave(duration=1.0, framerate=11025) corr = serial_corr(wave) corrs.append(corr) thinkplot.plot(betas, corrs) thinkplot.config(xlabel=r'pink noise parameter, $\beta$', ylabel='serial correlation', ylim=[0, 1.05]) thinkplot.save(root='autocorr3')
def generate_pmf(fb, hk): pmf_fb = Pmf(degrees(fb)) pmf_hk = Pmf(degrees(hk)) thinkplot.preplot(cols=2) thinkplot.plot([30, 2000], [5e-2, 2e-4], color='gray', linestyle='dashed') thinkplot.Pdf(pmf_fb, style='.', label='Facebook') thinkplot.config(xscale='log', yscale='log', xlabel='degree', ylabel='PMF') thinkplot.subplot(2) thinkplot.plot([55, 500], [5e-2, 2e-4], color='gray', linestyle='dashed') thinkplot.Pdf(pmf_hk, style='.', label='HK graph') thinkplot.config(xscale='log', yscale='log', xlabel='degree', ylabel='PMF') plt.savefig('PMFGraphs_Original.png')
def sweep_immunity(path, n, frames): """Sweeps the immunity parameter and creates a graph of the number of agents who are sick or contagious versus time for each immunity value. """ for immunity in [0.3, 0.5, 0.8, 0.99]: grid = World(n=n, immunity=immunity) # the World step method must return a value for this to work. In this # case, it returns the number that is either sick or contagious. segs = [grid.step() for i in range(frames)] thinkplot.plot(segs, label='immunity = %.1f' % immunity) thinkplot.config(xlabel='Time steps', ylabel='num_sick + num_contagious', loc='lower right') plt.savefig(path + 'immunity_sweep.pdf') plt.clf() print("Immunity sweep saved!")
def filter_wave(wave, start, duration, cutoff): """Selects a segment from the wave and filters it. Plots the spectrum and displays an Audio widget. wave: Wave object start: time in s duration: time in s cutoff: frequency in Hz """ segment = wave.segment(start, duration) spectrum = segment.make_spectrum() spectrum.plot(high=5000, color='0.7') spectrum.low_pass(cutoff) spectrum.plot(high=5000, color='#045a8d') thinkplot.config(xlabel='Frequency (Hz)') audio = spectrum.make_wave().make_audio() display(audio)
def voice_analysis_test(filepath): f = thinkdsp.read_wave(filepath) # spectrum = f.make_spectrum() # spectrum.plot() # the squiggly blue line # sp = spectrum.peaks() fig, ax = plt.subplots() # ok, i am currently not recording x=frequency, y=occurrences like ithought i was f.make_spectrum().plot(high=2000) thinkplot.config(title='Spectrum', xlabel='frequency (Hz)', ylabel='number occurrences') thinkplot.show() f_spectrogram = f.make_spectrogram(seg_length=1024) f_spectrogram.plot(high=2000) thinkplot.config(title='Spectrogram', xlabel='frequency (Hz)', ylabel='number occurrences') thinkplot.show()
def plot_correlate(): """Plots the autocorrelation function computed by numpy. """ wave = thinkdsp.read_wave('28042__bcjordan__voicedownbew.wav') wave.normalize() segment = wave.segment(start=0.2, duration=0.01) lags, corrs = autocorr(segment) corrs2 = numpy.correlate(segment.ys, segment.ys, mode='same') thinkplot.plot(corrs2) thinkplot.config(xlabel='lag', ylabel='correlation', xlim=[0, len(corrs2)]) thinkplot.save(root='autocorr9') N = len(corrs2) half = corrs2[N // 2:] lengths = range(N, N // 2, -1) half /= lengths half /= half[0]
def plot_fft_convolve(): """Makes a plot showing that FFT-based convolution works. """ df = pandas.read_csv('coindesk-bpi-USD-close.csv', nrows=1625, parse_dates=[0]) ys = df.Close.values # compute a 30-day average using numpy.convolve window = scipy.signal.gaussian(M=30, std=6) window /= window.sum() smoothed = numpy.convolve(ys, window, mode='valid') # compute the same thing using fft_convolve padded = zero_pad(window, len(ys)) smoothed2 = fft_convolve(ys, padded) M = len(window) smoothed2 = smoothed2[M - 1:] # check for the biggest difference diff = smoothed - smoothed2 print(max(abs(diff))) # compute autocorrelation using numpy.correlate N = len(ys) corrs = numpy.correlate(ys, ys, mode='same') corrs = corrs[N // 2:] corrs2 = fft_autocorr(ys) corrs2 = corrs2[N // 2:] # check for the biggest difference diff = corrs - corrs2 print(max(abs(diff))) # plot the results thinkplot.preplot(1) thinkplot.plot(corrs, color='0.7', linewidth=7, label='numpy.convolve') thinkplot.plot(corrs2.real, linewidth=2, label='fft_convolve') thinkplot.config(xlabel='lags', ylabel='correlation', xlim=[0, N // 2]) thinkplot.save(root='convolution9')
def plot_gaussian(): """Makes a plot showing the effect of convolution with a boxcar window. """ # start with a square signal signal = thinkdsp.SquareSignal(freq=440) wave = signal.make_wave(duration=1, framerate=44100) spectrum = wave.make_spectrum() # and a boxcar window boxcar = numpy.ones(11) boxcar /= sum(boxcar) # and a gaussian window gaussian = scipy.signal.gaussian(M=11, std=2) gaussian /= sum(gaussian) thinkplot.preplot(2) thinkplot.plot(boxcar, label="boxcar") thinkplot.plot(gaussian, label="Gaussian") thinkplot.config(xlabel="index", ylabel="amplitude") thinkplot.save(root="convolution7") ys = numpy.convolve(wave.ys, gaussian, mode="same") smooth = thinkdsp.Wave(ys, framerate=wave.framerate) spectrum2 = smooth.make_spectrum() # plot the ratio of the original and smoothed spectrum amps = spectrum.amps amps2 = spectrum2.amps ratio = amps2 / amps ratio[amps < 560] = 0 # plot the same ratio along with the FFT of the window padded = zero_pad(gaussian, len(wave)) dft_gaussian = numpy.fft.rfft(padded) thinkplot.plot(abs(dft_gaussian), color="0.7", label="Gaussian filter") thinkplot.plot(ratio, label="amplitude ratio") thinkplot.config(xlabel="frequency (Hz)", ylabel="amplitude ratio", xlim=[0, 22050], legend=False) thinkplot.save(root="convolution8")
def plot_wave_and_spectrum(wave, root): """Makes a plot showing a wave and its spectrum. wave: Wave object root: string used to generate filenames """ thinkplot.preplot(cols=2) wave.plot() thinkplot.config(xlabel='Time (days)', ylabel='Price ($)') thinkplot.subplot(2) spectrum = wave.make_spectrum() print(spectrum.estimate_slope()) spectrum.plot() thinkplot.config(xlabel='Frequency (1/days)', ylabel='Amplitude', xlim=[0, spectrum.fs[-1]], xscale='log', yscale='log') thinkplot.save(root=root)
def graph_bins(path, n, frames): """Creates a graph of how many agents are in each bin (healthy, sick, contagious) over time. """ world = World(n=n) for i in range(frames): world.step() thinkplot.plot(world.healthy, label='healthy') thinkplot.plot(world.sick, label='sick') thinkplot.plot(world.contagious, label='contagious') thinkplot.config(xlabel='Time steps', ylabel='num_agents', loc='lower right') plt.savefig(path + 'bin_graph.pdf') plt.clf() print("Bin graph saved!")
def plot_wave_and_spectrum(wave, root): """Makes a plot showing """ thinkplot.preplot(cols=2) wave.plot() thinkplot.config(xlabel='days', xlim=[0, 1650], ylabel='dollars', legend=False) thinkplot.subplot(2) spectrum = wave.make_spectrum() print(spectrum.estimate_slope()) spectrum.plot() thinkplot.config(xlabel='frequency (1/days)', ylabel='power', xscale='log', yscale='log', legend=False) thinkplot.save(root=root)
def window_plot(): """Makes a plot showing a sinusoid, hamming window, and their product. """ signal = thinkdsp.SinSignal(freq=440) duration = signal.period * 10.25 wave1 = signal.make_wave(duration) wave2 = signal.make_wave(duration) ys = numpy.hamming(len(wave1.ys)) window = thinkdsp.Wave(ys, wave1.framerate) wave2.hamming() thinkplot.preplot(rows=3, cols=1) pyplot.subplots_adjust(wspace=0.3, hspace=0.3, right=0.95, left=0.1, top=0.95, bottom=0.05) thinkplot.subplot(1) wave1.plot() thinkplot.config(axis=[0, duration, -1.07, 1.07]) thinkplot.subplot(2) window.plot() thinkplot.config(axis=[0, duration, -1.07, 1.07]) thinkplot.subplot(3) wave2.plot() thinkplot.config(axis=[0, duration, -1.07, 1.07], xlabel='time (s)') thinkplot.save(root='windowing2')
def three_spectrums(): """Makes a plot showing three spectrums for a sinusoid. """ thinkplot.preplot(rows=1, cols=3) pyplot.subplots_adjust(wspace=0.3, hspace=0.4, right=0.95, left=0.1, top=0.95, bottom=0.05) xticks = range(0, 900, 200) thinkplot.subplot(1) thinkplot.config(xticks=xticks) discontinuity(num_periods=30, hamming=False) thinkplot.subplot(2) thinkplot.config(xticks=xticks) discontinuity(num_periods=30.25, hamming=False) thinkplot.subplot(3) thinkplot.config(xticks=xticks) discontinuity(num_periods=30.25, hamming=True) thinkplot.save(root='windowing1')
def plot_beeps(): wave = thinkdsp.read_wave('253887__themusicalnomad__positive-beeps.wav') wave.normalize() thinkplot.preplot(3) # top left ax1 = plt.subplot2grid((4, 2), (0, 0), rowspan=2) plt.setp(ax1.get_xticklabels(), visible=False) wave.plot() thinkplot.config(title='Input waves', legend=False) # bottom left imp_sig = thinkdsp.Impulses([0.01, 0.4, 0.8, 1.2], amps=[1, 0.5, 0.25, 0.1]) impulses = imp_sig.make_wave(start=0, duration=1.3, framerate=wave.framerate) ax2 = plt.subplot2grid((4, 2), (2, 0), rowspan=2, sharex=ax1) impulses.plot() thinkplot.config(xlabel='Time (s)') # center right convolved = wave.convolve(impulses) ax3 = plt.subplot2grid((4, 2), (1, 1), rowspan=2) plt.title('Convolution') convolved.plot() thinkplot.config(xlabel='Time (s)') thinkplot.save(root='sampling1', formats=FORMATS, legend=False)
def plot_response(): thinkplot.preplot(cols=2) response = thinkdsp.read_wave('180961__kleeb__gunshots.wav') response = response.segment(start=0.26, duration=5.0) response.normalize() response.plot() thinkplot.config(xlabel='time', xlim=[0, 5.0], ylabel='amplitude', ylim=[-1.05, 1.05]) thinkplot.subplot(2) transfer = response.make_spectrum() transfer.plot() thinkplot.config(xlabel='frequency', xlim=[0, 22500], ylabel='amplitude') thinkplot.save(root='systems6') wave = thinkdsp.read_wave('92002__jcveliz__violin-origional.wav') wave.ys = wave.ys[:len(response)] wave.normalize() spectrum = wave.make_spectrum() output = (spectrum * transfer).make_wave() output.normalize() wave.plot(color='0.7') output.plot(alpha=0.4) thinkplot.config(xlabel='time', xlim=[0, 5.0], ylabel='amplitude', ylim=[-1.05, 1.05]) thinkplot.save(root='systems7')
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) 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)', ylabel='Amplitude')
def window_plot(): """Makes a plot showing a sinusoid, hamming window, and their product. """ signal = thinkdsp.SinSignal(freq=440) duration = signal.period * 10.25 wave1 = signal.make_wave(duration) wave2 = signal.make_wave(duration) ys = np.hamming(len(wave1.ys)) ts = wave1.ts window = thinkdsp.Wave(ys, ts, wave1.framerate) wave2.hamming() thinkplot.preplot(rows=3, cols=1) pyplot.subplots_adjust(wspace=0.3, hspace=0.3, right=0.95, left=0.1, top=0.95, bottom=0.05) thinkplot.subplot(1) wave1.plot() thinkplot.config(axis=[0, duration, -1.07, 1.07]) thinkplot.subplot(2) window.plot() thinkplot.config(axis=[0, duration, -1.07, 1.07]) thinkplot.subplot(3) wave2.plot() thinkplot.config(axis=[0, duration, -1.07, 1.07], xlabel='Time (s)') thinkplot.save(root='windowing2')
def plot_fft_convolve(): """Makes a plot showing that FFT-based convolution works. """ names = ['date', 'open', 'high', 'low', 'close', 'volume'] df = pd.read_csv('fb.csv', header=0, names=names, parse_dates=[0]) close = df.close.values[::-1] # compute a 30-day average using np.convolve window = scipy.signal.gaussian(M=30, std=6) window /= window.sum() smoothed = np.convolve(close, window, mode='valid') # compute the same thing using fft_convolve N = len(close) padded = thinkdsp.zero_pad(window, N) M = len(window) smoothed4 = fft_convolve(close, padded)[M-1:] # check for the biggest difference diff = smoothed - smoothed4 print(max(abs(diff))) # compute autocorrelation using np.correlate corrs = np.correlate(close, close, mode='same') corrs2 = fft_autocorr(close) # check for the biggest difference diff = corrs - corrs2 print(max(abs(diff))) # plot the results lags = np.arange(N) - N//2 thinkplot.plot(lags, corrs, color=GRAY, linewidth=7, label='np.convolve') thinkplot.plot(lags, corrs2.real, linewidth=2, label='fft_convolve') thinkplot.config(xlabel='Lag', ylabel='Correlation', xlim=[-N//2, N//2]) thinkplot.save(root='convolution9')
def plot_bitcoin(): """Plot BitCoin prices and a smoothed time series. """ nrows = 1625 df = pandas.read_csv('coindesk-bpi-USD-close.csv', nrows=nrows, parse_dates=[0]) ys = df.Close.values window = np.ones(30) window /= sum(window) smoothed = np.convolve(ys, window, mode='valid') N = len(window) smoothed = thinkdsp.shift_right(smoothed, N//2) thinkplot.plot(ys, color='0.7', label='daily') thinkplot.plot(smoothed, label='30 day average') thinkplot.config(xlabel='time (days)', ylabel='price', xlim=[0, nrows], loc='lower right') thinkplot.save(root='convolution1')
def plot_gaussian_noise(): """Shows the distribution of the spectrum of Gaussian noise. """ thinkdsp.random_seed(18) signal = thinkdsp.UncorrelatedGaussianNoise() wave = signal.make_wave(duration=0.5, framerate=48000) spectrum = wave.make_spectrum() thinkplot.preplot(2, cols=2) thinkstats2.NormalProbabilityPlot(spectrum.real, label='real') thinkplot.config(xlabel='Normal sample', ylabel='Amplitude', ylim=[-250, 250], loc='lower right') thinkplot.subplot(2) thinkstats2.NormalProbabilityPlot(spectrum.imag, label='imag') thinkplot.config(xlabel='Normal sample', ylim=[-250, 250], loc='lower right') thinkplot.save(root='noise1')
def plot_filter(M=11, std=2): signal = thinkdsp.SquareSignal(freq=440) wave = signal.make_wave(duration=1, framerate=44100) spectrum = wave.make_spectrum() gaussian = scipy.signal.gaussian(M=M, std=std) gaussian /= sum(gaussian) high = gaussian.max() thinkplot.preplot(cols=2) thinkplot.plot(gaussian) thinkplot.config(xlabel='Index', ylabel='Window', xlim=[0, len(gaussian) - 1], ylim=[0, 1.1 * high]) ys = np.convolve(wave.ys, gaussian, mode='same') smooth = thinkdsp.Wave(ys, framerate=wave.framerate) spectrum2 = smooth.make_spectrum() # plot the ratio of the original and smoothed spectrum amps = spectrum.amps amps2 = spectrum2.amps ratio = amps2 / amps ratio[amps < 560] = 0 # plot the same ratio along with the FFT of the window padded = thinkdsp.zero_pad(gaussian, len(wave)) dft_gaussian = np.fft.rfft(padded) thinkplot.subplot(2) thinkplot.plot(abs(dft_gaussian), color=GRAY, label='Gaussian filter') thinkplot.plot(ratio, label='amplitude ratio') thinkplot.show(xlabel='Frequency (Hz)', ylabel='Amplitude ratio', xlim=[0, 22050], ylim=[0, 1.05])
def plot_sincs(wave): start = 1.0 duration = 0.01 factor = 4 short = wave.segment(start=start, duration=duration) #short.plot() sampled = sample(short, factor) #sampled.plot_vlines(color='gray') spectrum = sampled.make_spectrum(full=True) boxcar = make_boxcar(spectrum, factor) sinc = boxcar.make_wave() sinc.shift(sampled.ts[0]) sinc.roll(len(sinc)//2) thinkplot.preplot(2, cols=2) sinc.plot() thinkplot.config(xlabel='Time (s)') thinkplot.subplot(2) boxcar.plot() thinkplot.config(xlabel='Frequency (Hz)', ylim=[0, 1.05], xlim=[-boxcar.max_freq, boxcar.max_freq]) thinkplot.save(root='sampling6', formats=FORMATS) return # CAUTION: don't call plot_sinc_demo with a large wave or it will # fill memory and crash plot_sinc_demo(short, 4) thinkplot.config(xlabel='Time (s)') thinkplot.save(root='sampling7', formats=FORMATS) start = short.start + 0.004 duration = 0.00061 plot_sinc_demo(short, 4, start, duration) thinkplot.config(xlabel='Time (s)', xlim=[start, start+duration], ylim=[-0.06, 0.17], legend=False) thinkplot.save(root='sampling8', formats=FORMATS)