import matplotlib.pyplot as plt from thinkdsp import decorate from thinkdsp import TriangleSignal def filter_spectrum(spectrum): """Divides the spectrum through by the fs. spectrum: Spectrum object """ # avoid division by 0 spectrum.hs[1:] /= spectrum.fs[1:] spectrum.hs[0] =0 wave = TriangleSignal(freq=440).make_wave(duration=0.5) wave.make_audio() spectrum = wave.make_spectrum() spectrum.plot(high=10000, color='gray') filter_spectrum(spectrum) spectrum.scale(440) spectrum.plot(high=10000) decorate(xlabel='Frequency (Hz)') filtered = spectrum.make_wave() filtered.make_audio() wave.write(filename='6.9.3.wav') plt.show()
spectrum.hs[1:] /= spectrum.fs[1:] spectrum.hs[0] = 0 def play(file, flags): print('Now play ' + file) PlaySound(file, flags) print('end') trangle = TriangleSignal(freq=440).make_wave(duration=0.01, framerate=10000) plt.subplot(231) plt.title("trangle") trangle.plot() trangle.write("三角波声音.wav") play("三角波声音.wav", flags=1) square = SquareSignal(freq=440).make_wave(duration=0.01, framerate=10000) plt.subplot(232) plt.title("square") square.plot() square.write("方波声音.wav") play("方波声音.wav", flags=1) sawtooth = SawtoothSignal(freq=440).make_wave(duration=0.01, framerate=10000) plt.subplot(233) plt.title("sawtooth") sawtooth.plot() sawtooth.write("斜波声音.wav") play("斜波声音.wav", flags=1) spectrum_tra = trangle.make_spectrum()