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 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_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_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 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)', ylabel='Amplitude')
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 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 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 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 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_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 process_noise(signal, root='white'): """Plots wave and spectrum for noise signals. signal: Signal root: string used to generate file names """ framerate = 48000 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_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_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)
def show_impulses(wave, factor, i): thinkplot.subplot(i) thinkplot.preplot(2) impulses = make_impulses(wave, factor) impulses.segment(0, 0.001).plot_vlines(linewidth=2, xfactor=1000) if i == 1: thinkplot.config(title='Impulse train', ylim=[0, 1.05]) else: thinkplot.config(xlabel='Time (ms)', ylim=[0, 1.05]) thinkplot.subplot(i + 1) impulses.make_spectrum(full=True).plot() kill_yticklabels() if i == 1: thinkplot.config(title='DFT of impulse train', xlim=[-22400, 22400]) else: thinkplot.config(xlabel='Frequency (Hz)', xlim=[-22400, 22400])
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 plot_sampling3(wave, root): ax1 = thinkplot.preplot(6, rows=3) wave.make_spectrum(full=True).plot(label='spectrum') thinkplot.config(xlim=XLIM, xticklabels='invisible') impulses = make_impulses(wave, 4) ax2 = thinkplot.subplot(2) sampled = wave * impulses spectrum = sampled.make_spectrum(full=True) spectrum.plot(label='sampled') thinkplot.config(xlim=XLIM, xticklabels='invisible') ax3 = thinkplot.subplot(3) spectrum.low_pass(5512.5) spectrum.plot(label='filtered') thinkplot.config(xlim=XLIM, xlabel='Frequency (Hz)') thinkplot.save(root=root, formats=FORMATS)
def plot_response(response): """Plots an input wave and the corresponding output. """ thinkplot.preplot(cols=2) response.plot() thinkplot.config(xlabel='Time (s)', xlim=[0.26, response.end], ylim=[-1.05, 1.05]) thinkplot.subplot(2) transfer = response.make_spectrum() transfer.plot() thinkplot.config(xlabel='Frequency (Hz)', xlim=[0, 22500], ylabel='Amplitude') thinkplot.save(root='systems6') violin = thinkdsp.read_wave('92002__jcveliz__violin-origional.wav') start = 0.11 violin = violin.segment(start=start) violin.shift(-start) violin.truncate(len(response)) violin.normalize() spectrum = violin.make_spectrum() output = (spectrum * transfer).make_wave() output.normalize() thinkplot.preplot(rows=2) violin.plot(label='input') thinkplot.config(xlim=[0, violin.end], ylim=[-1.05, 1.05]) thinkplot.subplot(2) output.plot(label='output') thinkplot.config(xlabel='Time (s)', xlim=[0, violin.end], ylim=[-1.05, 1.05]) thinkplot.save(root='systems7')
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 show_impulses(wave, factor, i): thinkplot.subplot(i) thinkplot.preplot(2) impulses = make_impulses(wave, factor) impulses.segment(0, 0.001).plot_vlines(linewidth=2) if i == 1: thinkplot.config(title='Impulse train', ylim=[0, 1.05]) else: thinkplot.config(xlabel='Time (s)', ylim=[0, 1.05]) thinkplot.subplot(i+1) impulses.make_spectrum(full=True).plot() kill_yticklabels() if i == 1: thinkplot.config(title='DFT of impulse train', xlim=[-22400, 22400]) else: thinkplot.config(xlabel='Frequency (Hz)', xlim=[-22400, 22400])
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_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 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 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_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=11025) 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_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_am(): wave = thinkdsp.read_wave('105977__wcfl10__favorite-station.wav') wave.unbias() wave.normalize() # top ax1 = thinkplot.preplot(6, rows=4) spectrum = wave.make_spectrum(full=True) spectrum.plot(label='wave') xlim = [-22050, 22050] thinkplot.config(xlim=xlim, xticklabels='invisible') #second carrier_sig = thinkdsp.CosSignal(freq=10000) carrier_wave = carrier_sig.make_wave(duration=wave.duration, framerate=wave.framerate) modulated = wave * carrier_wave ax2 = thinkplot.subplot(2, sharey=ax1) modulated.make_spectrum(full=True).plot(label='modulated') thinkplot.config(xlim=xlim, xticklabels='invisible') # third demodulated = modulated * carrier_wave demodulated_spectrum = demodulated.make_spectrum(full=True) ax3 = thinkplot.subplot(3, sharey=ax1) demodulated_spectrum.plot(label='demodulated') thinkplot.config(xlim=xlim, xticklabels='invisible') #fourth ax4 = thinkplot.subplot(4, sharey=ax1) demodulated_spectrum.low_pass(10000) demodulated_spectrum.plot(label='filtered') thinkplot.config(xlim=xlim, xlabel='Frequency (Hz)') thinkplot.save(root='sampling2', formats=FORMATS)
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 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_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 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 = xrange(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")
thinkplot.Config(xlabel = 'Total Revenue', ylabel = 'PMF') # In[54]: thinkplot.Pmf(flfindistdfpmf) thinkplot.Pmf(vfindistdfpmf) # In[55]: thinkplot.PrePlot(2) thinkplot.subplot(2) #axis = [0, 800, 0, 0.0005] thinkplot.Pmfs([flfindistdfpmf,vfindistdfpmf ]) thinkplot.Show(xlabel = 'Total Revenue', ylabel = 'PMF') # # Lets plot PMF of log transformed columns # In[56]: findistdf.columns # In[57]:
from __future__ import print_function, division import sys sys.path.append('../code') import thinkdsp import thinkplot import numpy as np N = 100 n = np.arange(1, N) thinkplot.preplot(rows=1, cols=3) thinkplot.subplot(1) thinkplot.plot(n, n**3, label='n**3', linewidth=1, color='r') thinkplot.plot(n, n**2, label='n**2', linewidth=1, color='b') thinkplot.plot(n, n * np.log(n), label='n*log(n)', linewidth=1, color='g') thinkplot.config(xlabel='n') thinkplot.subplot(2) thinkplot.plot(n, n**2, label='n**2', linewidth=1, color='b') thinkplot.plot(n, n * np.log(n), label='n*log(n)', linewidth=1, color='g') thinkplot.config(xlabel='n') thinkplot.subplot(3) thinkplot.plot(n, n, label='n', linewidth=1, color='r') thinkplot.plot(n, np.log(n), label='log(n)', linewidth=1, color='b') thinkplot.config(xlabel='n') thinkplot.show()