from thinkdsp import Spectrum, TriangleSignal, SquareSignal from thinkdsp import decorate import matplotlib.pyplot as plt import numpy as np triangle = TriangleSignal(440).make_wave(duration=0.01) plt.subplot(2, 1, 1) triangle.plot() decorate(xlabel="time(s)") spectrum = triangle.make_spectrum() print(spectrum.hs[0]) spectrum.hs[0] = 100 plt.subplot(2, 1, 2) triangle.plot(color="gray") spectrum.make_wave().plot() decorate(xlabel='Time(s)') plt.show()
from thinkdsp import TriangleSignal from thinkdsp import decorate import matplotlib.pyplot as plt triangle = TriangleSignal().make_wave(duration=0.01) #triangle.plot() #decorate(xlabel='Time (s)') #plt.show() spectrum = triangle.make_spectrum() #print(spectrum.hs[0]) spectrum.hs[0] = 100 triangle.plot(color='gray') spectrum.make_wave().plot() plt.show()
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()
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() filter_spectrum(spectrum2) spectrum2.scale(440) plt.subplot(3,3,4) spectrum2.plot(high=10000, color='red') plt.title("方波") plt.subplot(3,3,5) wave2.plot(color='red') plt.title('方波变换前')
import numpy as np import matplotlib.pyplot as plt from thinkdsp import TriangleSignal from thinkdsp import decorate from thinkdsp import SinSignal triangle = TriangleSignal(440).make_wave(duration=0.01) plt.subplot(2, 1, 1) triangle.plot() decorate(xlabel='Time (s)') spectrum = triangle.make_spectrum() spectrum.hs[0] print(spectrum.hs[0]) spectrum.hs[0] = 100 plt.subplot(2, 1, 2) triangle.plot(color='red') spectrum.make_wave().plot() decorate(xlabel='Time (s)') plt.show()
# avoid division by 0 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)