def test_square(): signal = SquareSignal(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)')
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')
from thinkdsp import normalize, unbias import numpy as np import matplotlib.pyplot as plt from thinkdsp import SquareSignal 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 plt.rcParams['font.sans-serif'] = ['SimHei'] #加上这一句就能在图表中显示中文 plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号 plt.title("方波") square = SquareSignal(1100).make_wave(duration=0.5, framerate=10000) wave1 = square.make_spectrum() square.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') wave = wave1.make_wave() wave.write(filename="sound2-3.wav") plt.show()
import sys sys.path.insert(1, 'dsp-modulo') from thinkdsp import SinSignal from thinkdsp import SquareSignal from thinkdsp import decorate import thinkplot import numpy senalRectangular = SquareSignal(freq=2, amp=1.0, offset=numpy.pi) waveRectangular = senalRectangular.make_wave(duration=1, start=0, framerate=44100) senalSenoidal = SinSignal(freq=2, amp=1, offset=0) harmonico1 = SinSignal(freq=senalSenoidal.freq * 3, amp=0.3, offset=0) harmonico2 = SinSignal(freq=senalSenoidal.freq * 5, amp=0.25, offset=0) harmonico3 = SinSignal(freq=senalSenoidal.freq * 7, amp=0.20, offset=0) harmonico4 = SinSignal(freq=senalSenoidal.freq * 9, amp=0.15, offset=0) harmonico5 = SinSignal(freq=senalSenoidal.freq * 11, amp=0.1, offset=0) harmonico6 = SinSignal(freq=senalSenoidal.freq * 13, amp=0.05, offset=0) waveSenoidal = senalSenoidal.make_wave(duration=1, start=0, framerate=44100) mezcla = senalSenoidal + harmonico1 + harmonico2 + harmonico3 + harmonico4 + harmonico5 + harmonico6 waveMezcla = mezcla.make_wave(duration=1, start=0, framerate=44100) waveRectangular.plot() waveMezcla.plot() thinkplot.show() espectroRectangular = waveRectangular.make_spectrum() espectroMezcla = waveMezcla.make_spectrum()
from thinkdsp import SawtoothSignal import matplotlib.pyplot as plt from thinkdsp import decorate from thinkdsp import SquareSignal signal = SquareSignal(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() wave.make_audio() wave.write(filename='6.9.2.wav') spectrum = wave.make_spectrum() spectrum.plot() decorate(xlabel='Frequency (Hz)') plt.show()
import matplotlib.pyplot as plt from thinkdsp import TriangleSignal from thinkdsp import decorate from thinkdsp import SquareSignal signal = SquareSignal(1100) duration = signal.period*3 segment = signal.make_wave(duration, framerate=10000) decorate(xlabel='Time (s)') plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False plt.subplot(121) segment.plot() wave = signal.make_wave(duration=0.5, framerate=10000) wave.apodize() wave.make_audio() spectrum = wave.make_spectrum() decorate(xlabel='Frequency (Hz)') plt.subplot(122) spectrum.plot() plt.show()
from thinkdsp import TriangleSignal, SquareSignal from thinkdsp import decorate import matplotlib.pyplot as plt import numpy as np square = SquareSignal(1100) cysquare = square.make_wave(duration=2, framerate=10000) cysquare.play('temp11.wav') square = square.make_wave(duration=0.5, framerate=10000) square.make_spectrum().plot() decorate(xlabel='Frequency (Hz)') from thinkdsp import SinSignal sin = SinSignal(500).make_wave(duration=2, framerate=10000) sin.play('temp22.wav') plt.show()
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()
def spect(spectrum): spectrum.hs[1:] /= spectrum.fs[1:] spectrum.hs[0] = 0 #三角波验证 wave = TriangleSignal(440).make_wave(duration=0.5) spectrum = wave.make_spectrum() plt.subplot(3, 1, 1) spectrum.plot(high=10000) spect(spectrum) plt.ylabel('三角波验证') #方波验证 wave = SquareSignal(440).make_wave(duration=0.5) spectrum = wave.make_spectrum() plt.subplot(3, 1, 2) spectrum.plot(high=10000) spect(spectrum) plt.ylabel('方波验证') #锯齿波验证 wave = SawtoothSignal(440).make_wave(duration=0.5) spectrum = wave.make_spectrum() plt.subplot(3, 1, 3) spectrum.plot(high=10000) spect(spectrum) plt.ylabel('方波验证') plt.show()
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() filter_spectrum(spectrum_tra) plt.subplot(234)
""" # avoid division by 0 spectrum.hs[1:] /= spectrum.fs[1:] 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)
plt.subplot(3,2,1) 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,2) spectrum.plot() decorate(xlabel='Frequency (Hz)') from thinkdsp import SquareSignal signal = SquareSignal(200)#绘制方波 duration = signal.period*3 segment = signal.make_wave(duration, framerate=10000) 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 TriangleSignal from thinkdsp import decorate import matplotlib.pyplot as plt import numpy as np from thinkdsp import SquareSignal signal = SquareSignal(1100) #绘制1100Hz方波信号 duration = signal.period * 3 segment = signal.make_wave(duration, framerate=10000) plt.subplot(2, 1, 1) segment.plot() decorate(xlabel='Time (s)') segment = signal.make_wave(duration=0.5, framerate=10000) spectrum = segment.make_spectrum() plt.subplot(2, 1, 2) spectrum.plot() decorate(xlabel='Frequency (Hz)') plt.show()