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()
cycles = self.freq * ts + self.offset / np.pi / 2 frac, _ = np.modf(cycles) ys = normalize(unbias(frac), self.amp) return ys plt.rcParams['font.sans-serif']=['SimHei'] #加上这一句就能在图表中显示中文 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 plt.subplot(311)#显示 plt.title("锯齿波") sawtooth = SawtoothSignal().make_wave(duration=0.5, framerate=40000) sawtooth.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') from thinkdsp import SquareSignal plt.subplot(312) plt.title("方波") sawtooth.make_spectrum().plot(color='gray') square = SquareSignal(amp=0.5).make_wave(duration=0.5, framerate=40000) square.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') from thinkdsp import TriangleSignal plt.subplot(313)#显示 plt.title("三角波") sawtooth.make_spectrum().plot(color='gray') triangle = TriangleSignal(amp=0.79).make_wave(duration=0.5, framerate=40000) triangle.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') plt.show()
import thinkdsp import matplotlib.pyplot as plt import numpy as np from scipy import signal from thinkdsp import SawtoothSignal, SquareSignal, TriangleSignal from thinkdsp import decorate plt.rcParams['font.sans-serif']=['SimHei']#图表上可以显示中文 plt.rcParams['axes.unicode_minus']=False#图表上可以显示负号 def filter_spectrum(spectrum): spectrum.hs[1:] /= spectrum.fs[1:] spectrum.hs[0] = 0 wave1= TriangleSignal(freq=440).make_wave(duration=0.5) spectrum1 = wave1.make_spectrum() filter_spectrum(spectrum1) spectrum1.scale(440) plt.subplot(3,3,1) spectrum1.plot(high=10000, color='blue') plt.title("三角波频谱") plt.subplot(3,3,2) wave1.plot(color='blue') plt.title('三角波变换前') plt.subplot(3,3,3) spectrum1.make_wave().plot(color='blue') plt.title("三角波变换后") wave2=SquareSignal(freq=440).make_wave(duration=0.5) spectrum2 = wave2.make_spectrum()
from thinkdsp import Sinusoid from thinkdsp import normalize, unbias from thinkdsp import TriangleSignal from thinkdsp import decorate from thinkdsp import SquareSignal import os import numpy as np import matplotlib.pyplot as plt #三角波 频率440HZ 波长0.01秒 triangle = TriangleSignal(440).make_wave(duration=0.01) triangle.plot() # 振幅为2v 相位为0 spectrum = triangle.make_spectrum() print(spectrum.hs[0]) #频率分量0 # 振幅为2v 垂直偏移1v spectrum.hs[0] = 100 #在频率为0处加上100分量 等于叠加一个100*(duration=0.01)==1v的直流信号 triangle.plot(color='gray') spectrum.make_wave().plot() plt.show()
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() filter_spectrum(spectrum_tra) plt.subplot(234) spectrum_tra.plot() spectrum_squ = square.make_spectrum() filter_spectrum(spectrum_squ) plt.subplot(235) spectrum_squ.plot() spectrum_saw = sawtooth.make_spectrum() filter_spectrum(spectrum_saw) plt.subplot(236) spectrum_saw.plot() plt.show()