def test_Sawtooth(): signal = SawtoothSignal(200) duration = signal.period * 3 segment = signal.make_wave(duration, framerate=10000) segment.plot() decorate(xlabel='Time (s)') wave = signal.make_wave(duration=0.5, framerate=10000) wave.apodize() spectrum = wave.make_spectrum() spectrum.plot() decorate(xlabel='Frequency (Hz)')
import matplotlib.pyplot as plt from thinkdsp import TriangleSignal,decorate,SquareSignal,SawtoothSignal signal = SawtoothSignal(200) duration = signal.period*3 segment = signal.make_wave(duration, framerate=10000) plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False decorate(xlabel='Time (s)') plt.subplot(121) segment.plot() wave = signal.make_wave(duration=0.5, framerate=10000) wave.apodize() wave.make_audio() spectrum = wave.make_spectrum() spectrum.plot() decorate(xlabel='Frequency (Hz)') plt.subplot(122) segment.plot() plt.show()
from thinkdsp import decorate from thinkdsp import Sinusoid from thinkdsp import normalize, unbias from thinkdsp import SinSignal from thinkdsp import SquareSignal from thinkdsp import TriangleSignal from thinkdsp import SawtoothSignal import numpy as np import matplotlib.pyplot as plt wave = TriangleSignal(freq=440).make_wave(duration=0.5) wave1 = SawtoothSignal(freq=440).make_wave(duration=0.5) wave2 = SquareSignal(freq=440).make_wave(duration=0.5) def filter_spectrum(spectrum): spectrum.hs[1:] /= spectrum.fs[1:] spectrum.hs[0] = 0 plt.subplot(311) spectrum = wave.make_spectrum() spectrum.plot(high=10000, color='gray') filter_spectrum(spectrum) spectrum.scale(440) spectrum.plot(high=10000) decorate(xlabel='Frequency (Hz)') plt.subplot(312) spectrum1 = wave1.make_spectrum() spectrum1.plot(high=10000, color='red')
class SawtoothSignal(Sinusoid): """Represents a sawtooth signal.""" def evaluate(self, ts): """Evaluates the signal at the given times. ts: float array of times returns: float wave array """ cycles = self.freq * ts + self.offset / np.pi / 2 frac, _ = np.modf(cycles) ys = normalize(unbias(frac), self.amp) return ys sawtooth = SawtoothSignal().make_wave(duration=0.5, framerate=40000) from thinkdsp import SquareSignal sawtooth.make_spectrum().plot(color='black') square = SquareSignal(amp=0.5).make_wave(duration=0.5, framerate=40000) square.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') sawtooth.make_spectrum().plot(color='yellow') sawtooth.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') from thinkdsp import TriangleSignal sawtooth.make_spectrum().plot(color='red') triangle = TriangleSignal(amp=0.79).make_wave(duration=0.5, framerate=40000) triangle.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') plt.show()
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('方波变换前') plt.subplot(3,3,6) spectrum2.make_wave().plot(color='red') plt.title("方变换后") spectrum1.make_wave.write(filename='output2-3.wav') wave3=SawtoothSignal(freq=440).make_wave(duration=0.5) spectrum3 = wave3.make_spectrum() filter_spectrum(spectrum3) spectrum3.scale(440) plt.subplot(3,3,7) spectrum3.plot(high=10000, color='green') plt.title("方波") plt.subplot(3,3,8) wave3.plot(color='green') plt.title('方波变换前') plt.subplot(3,3,9) spectrum3.make_wave().plot(color='green') plt.title("方变换后") plt.show()
from thinkdsp import SawtoothSignal, Signal, Spectrum import matplotlib.pyplot as plt from thinkdsp import decorate Signal = SawtoothSignal(200) duration = Signal.period * 3 segment = Signal.make_wave(duration, framerate=20000) segment.plot() decorate(xlabel='Time(s)') plt.show() plt.show() wave = Signal.make_wave(duration=0.5, framerate=10000) wave.apodize() wave.make_audio() wave.write(filename='2-2.wav') Spectrum = wave.make_spectrum() Spectrum.plot() decorate(xlabel='频谱(Hz)') plt.show()
from thinkdsp import decorate from thinkdsp import Sinusoid from thinkdsp import normalize, unbias from thinkdsp import SinSignal from thinkdsp import SquareSignal from thinkdsp import TriangleSignal from thinkdsp import SawtoothSignal import numpy as np import matplotlib.pyplot as plt sawtooth = SawtoothSignal().make_wave(duration=0.5, framerate=40000) sawtooth.make_audio() plt.subplot(311) sawtooth.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') plt.subplot(312) sawtooth.make_spectrum().plot(color='red') square = SquareSignal(amp=0.5).make_wave(duration=0.5, framerate=40000) square.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') plt.subplot(313) sawtooth.make_spectrum().plot(color='green') triangle = TriangleSignal(amp=0.79).make_wave(duration=0.5, framerate=40000) triangle.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') plt.show()
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() 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.plot() decorate(xlabel='Frequency (Hz)') wave.apodize() wave.make_audio() spectrum = f5(spectrum) plt.subplot(3, 2, 4) spectrum.plot() decorate(xlabel='Frequency (Hz)') wave = spectrum.make_wave() wave.apodize() wave.make_audio() signal = SawtoothSignal(1100) #锯齿波验证 duration = signal.period * 4 segment = signal.make_wave(duration, framerate=10000) wave = signal.make_wave(duration=1, framerate=10000) spectrum = wave.make_spectrum() plt.subplot(3, 2, 5) spectrum.plot() decorate(xlabel='Frequency (Hz)') wave.apodize() wave.make_audio() spectrum = f5(spectrum) plt.subplot(3, 2, 6) spectrum.plot() decorate(xlabel='Frequency (Hz)')
spectrum.hs[0] = 0 wave = TriangleSignal(freq=440).make_wave(duration=0.5) spectrum = wave.make_spectrum() plt.subplot(3, 1, 1) spectrum.plot(high=10000, color='red') filter_spectrum(spectrum) spectrum.scale(440) spectrum.plot(high=10000) decorate(xlabel='Frequency (Hz)') wave2 = square = SquareSignal(1100).make_wave(duration=0.5) spectrum2 = wave2.make_spectrum() plt.subplot(3, 1, 2) spectrum2.plot(high=10000, color='red') filter_spectrum(spectrum2) spectrum2.scale(440) spectrum2.plot(high=10000) decorate(xlabel='Frequency (Hz)') wave3 = sawtooth = SawtoothSignal().make_wave(duration=0.5) spectrum3 = wave3.make_spectrum() plt.subplot(3, 1, 3) spectrum3.plot(high=10000, color='red') filter_spectrum(spectrum3) spectrum3.scale(440) spectrum3.plot(high=10000) decorate(xlabel='Frequency (Hz)') plt.show()
plt.subplot(3,2,3) segment.plot() decorate(xlabel='Time (s)') wave = signal.make_wave(duration=0.5, framerate=10000) wave.apodize() wave.make_audio() spectrum = wave.make_spectrum()#绘制方波频谱 plt.subplot(3,2,4) spectrum.plot() decorate(xlabel='Frequency (Hz)') from thinkdsp import SawtoothSignal signal = SawtoothSignal(200)#绘制锯齿信号 duration = signal.period*3 segment = signal.make_wave(duration, framerate=10000) plt.subplot(3,2,5) segment.plot() decorate(xlabel='Time (s)') wave = signal.make_wave(duration=0.5, framerate=10000) wave.apodize() wave.make_audio() spectrum = wave.make_spectrum() plt.subplot(3,2,6) spectrum.plot() decorate(xlabel='Frequency (Hz)')