def main(): numpy.set_printoptions(precision=3, suppress=True) test_dct() return synthesize_example() return test_synthesize(fshift=0, tshift=0) test_synthesize(fshift=0.5, tshift=0.5) return cos_sig = thinkdsp.CosSignal(freq=1) wave = cos_sig.make_wave(duration=1, start=0, framerate=4) print wave.ys dct = scipy.fftpack.dct(wave.ys, type=2) print dct cos_trans = wave.cos_transform() xs, ys = zip(*cos_trans) print ys return framerate = 4000 cos_sig = (thinkdsp.CosSignal(freq=440) + thinkdsp.SinSignal(freq=660) + thinkdsp.CosSignal(freq=880)) wave = cos_sig.make_wave(duration=0.5, start=0, framerate=framerate) res = wave.cos_transform() for index in range(3): plot(res, index)
def step_4(): Sin1 = thinkdsp.CosSignal(freq=261.63, amp=0.5, offset=0) Sin2 = thinkdsp.CosSignal(freq=329.63, amp=0.5, offset=0) Sin3 = thinkdsp.CosSignal(freq=392.0, amp=0.5, offset=0) SumSignal = Sin1 + Sin2 + Sin3 SumSignal_wave = SumSignal.make_wave(duration=3, start=0, framerate=8000) period = SumSignal.period SumSignal_seg = SumSignal_wave.segment(start=0, duration=period * 5) SumSignal_seg.plot() tp.show() SumSignal_wave.write(filename='output.wav')
def main(): # test signal for proof of concept sig1 = thinkdsp.CosSignal(freq=20, amp=1.0) sig2 = thinkdsp.CosSignal(freq=2500, amp=0.05) sig3 = thinkdsp.CosSignal(freq=3000, amp=0.04) sig4 = thinkdsp.CosSignal(freq=3500, amp=0.03) sig = sig1 + sig2 + sig3 + sig4 # set sampling frequency framerate = 20000 # make a wave object from signal object wave = sig.make_wave(duration=1, start=0, framerate=framerate) demodulate_steps(wave=wave)
def mix_cosines(): """Plots three periods of a mix of cosines. """ # create a SumSignal cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0) mix = sin_sig + cos_sig # create a wave wave = mix.make_wave(duration=1.0, start=0, framerate=11025) print('Number of samples', len(wave)) print('Timestep in ms', 1000 / wave.framerate) assert len(wave) == wave.framerate # select a segment period = mix.period segment = wave.segment(start=0, duration=period * 3) # plot the segment thinkplot.preplot(1) segment.plot() thinkplot.Save(root='sounds4', xlabel='Time (s)', axis=[0, period * 3, -1.55, 1.55], formats=FORMATS, legend=False)
def broadcast(src_path, tag): #files are sourced and saved from/to the same path #normalizes amplitude os.chdir(src_path) for file in os.listdir(src_path): if file.endswith('.wav'): wave = td.read_wave(src_path + file) carrier_sig = td.CosSignal(freq=10000) carrier_wave = carrier_sig.make_wave(duration=wave.duration, framerate=wave.framerate) modulated = wave * carrier_wave demodulated = modulated * carrier_wave demodulated_spectrum = demodulated.make_spectrum(full=True) demodulated_spectrum.low_pass(10000) filtered = demodulated_spectrum.make_wave() filtered.write(filename='BC_' + file) #create json file if there is no existing json file provided if tag is not None: if file.endswith('.wav'): json_file_name = re.sub("\.wav", ".json", file) data = {'tags': [tag]} with open(src_path + 'BC_' + json_file_name, 'w') as outfile: json.dump(data, outfile) #copy json file if is provided elif tag is None: if file.endswith('.json'): shutil.copyfile(src_path + file, src_path + 'BC_' + file)
def mix_cosines(): """Demonstrates methods in the thinkdsp module. """ # create a SumSignal cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0) mix = sin_sig + cos_sig # create a wave wave = mix.make_wave(duration=0.5, start=0, framerate=11025) print 'Number of samples', len(wave.ys) print 'Timestep in ms', 1.0 / 11025 * 1000 # select a segment period = mix.period segment = wave.segment(start=0, duration=period*3) # plot the segment segment.plot() thinkplot.Save(root='example1', xlabel='time (s)', axis=[0, period*3, -1.55, 1.55]) # write the whole wave wave.normalize() wave.apodize() wave.write(filename='example1.wav') # play the wave thinkdsp.play_wave(filename='example1.wav', player='aplay')
def plot_segment(freq): signal = thinkdsp.CosSignal(freq) duration = signal.period * 4 thinkplot.Hlines(0, 0, duration, color='gray') segment = signal.make_wave(duration, framerate=framerate * 10) segment.plot(linewidth=0.5, color='gray') segment = signal.make_wave(duration, framerate=framerate) segment.plot_vlines(label=freq, linewidth=4)
def test2(): cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=800, amp=0.5, offset=0) mix = cos_sig + sin_sig wave = mix.make_wave(duration=2, start=0, framerate=16000) wave.normalize() # 归一化 wave.write(filename='play3.wav') wave.apodize() # 渐入渐出 wave.write(filename='play4.wav')
def aliasing_example(): framerate = 10000 thinkplot.preplot(num=2) freq1 = 4500 signal = thinkdsp.CosSignal(freq1) duration = signal.period*5 segment = signal.make_wave(duration, framerate=framerate) segment.plot(label=freq1) freq2 = 5500 signal = thinkdsp.CosSignal(freq2) #duration = signal.period*10 segment = signal.make_wave(duration, framerate=framerate) segment.plot(label=freq2) thinkplot.save(root='aliasing-3', xlabel='time (s)', axis=[0, duration, -1.05, 1.05] )
def my_gen_bpsk_1(bits, sample_rate=8000, symbol_period=.01, freq=1000): symbol_len = symbol_period * sample_rate square_signal = bits_to_signal(bits, symbol_len) transmission_duration = len(square_signal) * symbol_period cos_signal = thinkdsp.CosSignal(freq=freq, offset=0) print transmission_duration print sample_rate cos_wave = cos_signal.make_wave(duration=transmission_duration, framerate=sample_rate) print cos_wave print square_signal print len(cos_wave.ys) print len(square_signal) return np.multiply(cos_wave.ys, square_signal)
def synthesize1(amps, fs, ts): """Synthesize a mixture of cosines with given amps and fs. amps: amplitudes fs: frequencies in Hz ts: times to evaluate the signal returns: wave array """ components = [thinkdsp.CosSignal(freq, amp) for amp, freq in zip(amps, fs)] signal = thinkdsp.SumSignal(*components) ys = signal.evaluate(ts) return ys
def signal(offset): cos_sig = thinkdsp.CosSignal(freq=1.0, amp=1.0, offset=offset) sin_sig = thinkdsp.SinSignal(freq=1.0, amp=1.0, offset=offset) cos_wave = cos_sig.make_wave(duration=0.5, start=0, framerate=8000) sin_wave = sin_sig.make_wave(duration=0.5, start=0, framerate=8000) period = sin_sig.period cos_seg = cos_wave.segment(start=0, duration=period * 5) sin_seg = sin_wave.segment(start=0, duration=period * 5) sin_seg.plot() tp.show() cos_seg.plot() tp.show()
def aliasing_example(offset=0.000003): """Makes a figure showing the effect of aliasing. """ framerate = 10000 thinkplot.preplot(num=2) freq1 = 4500 signal = thinkdsp.CosSignal(freq1) duration = signal.period * 5 segment = signal.make_wave(duration, framerate=framerate) thinkplot.Hlines(0, 0, duration, color='gray') segment.shift(-offset) segment.plot_vlines(label=freq1, linewidth=3) freq2 = 5500 signal = thinkdsp.CosSignal(freq2) segment = signal.make_wave(duration, framerate=framerate) segment.shift(+offset) segment.plot_vlines(label=freq2, linewidth=3) thinkplot.save(root='aliasing1', xlabel='Time (s)', axis=[-0.00002, duration, -1.05, 1.05])
def test1(): cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0) mix = cos_sig + sin_sig wave = mix.make_wave(duration=10, start=0, framerate=16000) wave.normalize() # 归一化 wave.apodize() # 渐入渐出 wave.plot() plt.show() wave.write(filename='output.wav') # 显示原波形 violin_wave = thinkdsp.read_wave('output.wav') segment = violin_wave.segment(start=0, duration=2 / 440) segment.plot() plt.show() # 频谱 spectrum = violin_wave.make_spectrum() spectrum.plot() plt.show() # 低通 spectrumlow = spectrum.copy() spectrumlow.low_pass(cutoff=600, factor=0.01) spectrumlow.plot() plt.show() violin_wave = spectrumlow.make_wave() segment = violin_wave.segment(start=0, duration=2 / 440) segment.plot() plt.show() # 高通 spectrumhigh = spectrum.copy() spectrumhigh.high_pass(cutoff=600, factor=0.01) spectrumhigh.plot() plt.show() violin_wave = spectrumhigh.make_wave() segment = violin_wave.segment(start=0, duration=2 / 440) segment.plot() plt.show()
def plot_am(): wave = thinkdsp.read_wave('105977__wcfl10__favorite-station.wav') wave.unbias() wave.normalize() # top ax1 = thinkplot.preplot(6, rows=4) spectrum = wave.make_spectrum(full=True) spectrum.plot(label='wave') xlim = [-22050, 22050] thinkplot.config(xlim=xlim, xticklabels='invisible') #second carrier_sig = thinkdsp.CosSignal(freq=10000) carrier_wave = carrier_sig.make_wave(duration=wave.duration, framerate=wave.framerate) modulated = wave * carrier_wave ax2 = thinkplot.subplot(2, sharey=ax1) modulated.make_spectrum(full=True).plot(label='modulated') thinkplot.config(xlim=xlim, xticklabels='invisible') # third demodulated = modulated * carrier_wave demodulated_spectrum = demodulated.make_spectrum(full=True) ax3 = thinkplot.subplot(3, sharey=ax1) demodulated_spectrum.plot(label='demodulated') thinkplot.config(xlim=xlim, xticklabels='invisible') #fourth ax4 = thinkplot.subplot(4, sharey=ax1) demodulated_spectrum.low_pass(10000) demodulated_spectrum.plot(label='filtered') thinkplot.config(xlim=xlim, xlabel='Frequency (Hz)') thinkplot.save(root='sampling2', formats=FORMATS)
def testDct(self): signal = thinkdsp.CosSignal(freq=2) wave = signal.make_wave(duration=1, framerate=8) dct = wave.make_dct() self.assertAlmostEqual(dct.fs[0], 0.25)
def synthesize1(amps, fs, ts): components = [thinkdsp.CosSignal(freq, amp) for amp, freq in zip(amps, fs)] signal = thinkdsp.SumSignal(*components) ys = signal.evaluate(ts) return ys
from ipywidgets import interact, interactive, fixed import ipywidgets as widgets from IPython.display import display from matplotlib import pyplot def stretch(wave, stretch_factor): return thinkdsp.Wave(wave.ys, wave.ts / stretch_factor, framerate=wave.framerate * stretch_factor) start_time = datetime.datetime.now() cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.CosSignal(freq=1000, amp=0.5, offset=0) mix = sin_sig + cos_sig wave = mix.make_wave(duration=0.5, start=0, framerate=10000) period = mix.period segment = wave.segment(start=0, duration=period * 3) segment.plot() thinkplot.show() mix_spectrum = wave.make_spectrum() mix_spectrum.plot()
# plt.show() # # wave1 = signal1.make_wave(duration=0.5, framerate=10000) # spectrum1 = wave1.make_spectrum() # spectrum1.plot() # plt.show() # # ''' square signal analysis''' # signal2= thinkdsp.SquareSignal(100) # signal2.plot() # plt.show() # # wave2 = signal2.make_wave(duration=0.5, framerate=10000) # spectrum2 = wave2.make_spectrum() # spectrum2.plot() # plt.show() ''' aliasing analysis''' framerate = 10000 signal = thinkdsp.CosSignal(4500) duration = signal.period*5 segment = signal.make_wave(duration, framerate=framerate) segment.plot() plt.show() signal = thinkdsp.CosSignal(5500) segment = signal.make_wave(duration, framerate=framerate) segment.plot() plt.show()
# create a subplot ax = fig.add_subplot(223) # plot a wave new_wave.plot(label='new wave', color='b') ax.legend() # create a subplot ax = fig.add_subplot(224) # plot a spectrum new_spectrum.plot(label='new spec', color='b') ax.legend() plt.show() ''' # testing integration sig = thinkdsp.CosSignal(freq=1, amp=1, offset=0) wave = sig.make_wave(duration=1, start=0, framerate=100) spectrum = wave.make_spectrum() new_spectrum = spectrum.integrate() new_wave = new_spectrum.make_wave() # create a figure fig = plt.figure() # create a subplot ax = fig.add_subplot(221) # plot a wave wave.plot(label='wave', color='b') ax.legend() # create a subplot ax = fig.add_subplot(222) # plot a spectrum
import thinkdsp import matplotlib.pyplot as plt cos_signal = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sigal = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0) mix = sin_sigal + cos_signal wave = mix.make_wave(duration=0.5, start=0, framerate=11025) wave.plot() plt.show()
import thinkdsp import thinkplot import numpy cos_sig = thinkdsp.CosSignal(freq=440) cos_sig.plot() thinkplot.config(xlabel='time', legend=False)
#%matplotlib inline import thinkdsp import thinkplot import numpy as np from ipywidgets import interact, interactive, fixed import ipywidgets as widgets from IPython.display import display # Instantiate cosine and sine signals. # In[2]: cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0) # Plot the sine and cosine signals. By default, `plot` plots three periods. # In[3]: cos_sig.plot() thinkplot.config(xlabel='Time (s)') # Notice that the frequency of the sine signal is doubled, so the period is halved. # In[5]: sin_sig.plot() thinkplot.config(xlabel='Time (s)')
waveguitar.normalize() waveguitar.make_audio() waveguitar.plot() segment_guitar = waveguitar.segment(start=1.1, duration=1) segment_guitar.make_audio() segment_guitar.plot() segment_guitar.segment(start=1.1, duration=0.005).plot() spectrum_guitar = segment_guitar.make_spectrum() spectrum_guitar.plot(high=410) spectrum_guitar.low_pass(2000) spectrum_guitar.make_wave().make_audio() spectrum_guitar.peaks()[:10] cos_sig_sax = thinkdsp.CosSignal(freq=492, amp=1.0, offset=0) cos_sig_guitar = thinkdsp.CosSignal(freq=401, amp=1.0, offset=0) cos_sig = cos_sig_sax + cos_sig_guitar cos_sig.plot() thinkplot.config(xlabel='Time (s)') wave2 = cos_sig.make_wave(duration=1) wave2.apodize() wave2.make_audio()
import thinkdsp import matplotlib.pyplot as plt from winsound import PlaySound plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体楷体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 def play(file, flags): print('Now play ' + file) PlaySound(file, flags) print('end') sin = thinkdsp.SinSignal(freq=400, amp=1.0) cos = thinkdsp.CosSignal(freq=1200, amp=0.8) signal = cos + sin plt.subplot(121) plt.title("复合信号") signal.plot() wave = signal.make_wave(duration=1) spectrum = wave.make_spectrum() plt.subplot(122) plt.title("复合信号频谱") spectrum.plot(high=2000) wave.normalize() wave.write("复合信号.wav") play("复合信号.wav", flags=1) plt.show()
def get_cosine_wave(freq, offset, duration=0.1, framerate=40000): signal = thinkdsp.CosSignal(freq=freq, amp=1.0, offset=offset) wave = signal.make_wave(duration=duration, start=0, framerate=framerate) return wave.ys
import os import thinkdsp as dsp import matplotlib.pyplot as plt from winsound import PlaySound plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 def play(file, flags): print('Now play ' + file) PlaySound(file, flags) print('end') sin = dsp.SinSignal(freq=400, amp=1.0) cos = dsp.CosSignal(freq=1000, amp=0.5) signal = cos + sin plt.subplot(121) plt.title("复合信号") signal.plot() wave = signal.make_wave(duration=1) spectrum = wave.make_spectrum() plt.subplot(122) plt.title("复合信号频谱") spectrum.plot(high=2000) wave.normalize() wave.write("复合信号.wav") play("复合信号.wav", flags=1) plt.show()
import thinkdsp import matplotlib.pyplot as plt from winsound import PlaySound plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 def play(file, flags): print('Now play ' + file) PlaySound(file, flags) print('end') sin = thinkdsp.SinSignal(freq=400, amp=1.0) cos = thinkdsp.CosSignal(freq=1000, amp=0.5) signal = cos + sin plt.subplot(121) plt.title("复合信号") signal.plot() wave = signal.make_wave(duration=1) spectrum = wave.make_spectrum() plt.subplot(122) plt.title("复合信号频谱") spectrum.plot(high=2000) wave.normalize() wave.write("复合信号.wav") play("复合信号.wav", flags=1) plt.show()
import thinkdsp import matplotlib.pyplot as pyplot import random pyplot.gcf().clear() cos_sig = thinkdsp.CosSignal(5, 0.6, 0) sin_sig = thinkdsp.SinSignal(15, 0.5, 0) sin_sig2 = thinkdsp.SinSignal(25, 0.5, 0) sin_sig3 = thinkdsp.SinSignal(35, 0.9, 0) for x in range(1, 100, 1): sin_sig3 += thinkdsp.SinSignal(x, random.uniform(0.001, 0.11), 0) mix = cos_sig + sin_sig + sin_sig2 + sin_sig3 wave = mix.make_wave(100, 0, 11025) print(wave.ys) wave.plot() pyplot.show() period = mix.period segment = wave.segment(0, 3 * period) segment.plot() spectrum = wave.make_spectrum() spectrum.plot(high=100) pyplot.show() wave.write(filename='naveed.wav') # #pyplot.gcf().clear() #cos_sig = thinkdsp.CosSignal(5,0.6,0) #sin_sig = thinkdsp.SinSignal(15,0.5,0) #sin_sig2 = thinkdsp.SinSignal(25,0.5,0) #sin_sig3 = thinkdsp.SinSignal(35,0.5,0) #for x in range(1,100,1):