Esempio n. 1
0
def genToneByFreq(rootFreq, dur, partialAmp, partialRatio):
    #    sc = [0.7, 0.7, 0.7, 0.7, 0.7, 0.7]
    #    sc = [0.5, 0.7, 0.3, 0.2, 0.1, 0.1]
    #    sc = [0.5, 0.7, 0.7, 0.5, 0.4, 0.4, 0.5, 0.1, 0.3, 0.1, 0.1, 0.05]
    #    sc = [0.5]
    #    for i in range(len(sc)) :
    #        sc[i] = sc[i]*3
    #    sc = partialAmp

    sin_sig = thinkdsp.SinSignal(freq=rootFreq, amp=partialAmp[0], offset=0)
    wave = sin_sig.make_wave(duration=dur, start=0, framerate=11025)

    window = makeWindow(len(wave.ys), 100)
    wave.window(window)

    for i in range(1, len(partialAmp)):
        #        sin_sig = thinkdsp.SinSignal(freq=rootFreq * ratioArtSpec(i+1), amp=sc[i], offset=0)
        sin_sig = thinkdsp.SinSignal(freq=rootFreq * partialRatio[i],
                                     amp=partialAmp[i],
                                     offset=0)
        w = sin_sig.make_wave(duration=dur, start=0, framerate=11025)

        window = makeWindow(len(wave.ys), 100 - i * 10)
        w.window(window)
        wave = wave + w

    return (wave)
Esempio n. 2
0
def genToneByFreq(rootFreq, dur, ss):
    #    sc = [0.7, 0.7, 0.7, 0.7, 0.7, 0.7]
    #    sc = [0.5, 0.7, 0.3, 0.2, 0.1, 0.1]
    #    sc = [0.5, 0.7, 0.7, 0.5, 0.4, 0.4, 0.5, 0.1, 0.3, 0.1, 0.1, 0.05]
    #    sc = [0.5]
    #    for i in range(len(sc)) :
    #        sc[i] = sc[i]*3
    #    sc = partialAmp

    sin_sig = thinkdsp.SinSignal(freq=rootFreq, amp=ss.partialAmp[0], offset=0)
    wave = sin_sig.make_wave(duration=dur, start=0, framerate=11025)

    #    window = makeWindow(len(wave.ys),100)
    window = makeWindow(len(wave.ys), ss.cutpoint, 1, ss.sustainLevel)
    wave.window(window)

    l = len(ss.partialAmp)
    for i in range(1, l):
        #        sin_sig = thinkdsp.SinSignal(freq=rootFreq * ratioArtSpec(i+1), amp=sc[i], offset=0)
        sin_sig = thinkdsp.SinSignal(freq=rootFreq * ss.partialRatio[i],
                                     amp=ss.partialAmp[i],
                                     offset=0)
        w = sin_sig.make_wave(duration=dur, start=0, framerate=11025)

        #        window = makeWindow(len(wave.ys),100-i*10)
        r = 0.3 * ((l - i - 1) / (l - 1)) / l + 0.7
        #       r = 1
        window = makeWindow(len(wave.ys), ss.cutpoint, r, ss.sustainLevel)
        w.window(window)
        wave = wave + w

    return (wave)
Esempio n. 3
0
    def encodeWavFile(message):
        """
            returns: .wav file destination to the ecoded message
        """
        binNum = ""
        encodedWave = thinkdsp.SinSignal(freq=0, amp=0, offset=0).make_wave()
        offCount = 0
        for letter in message:
            for key, value in lettersDict.items():
                if letter == value:
                    binNum = key

            count = 0

            for num in binNum:
                if num == str(1):
                    signal = thinkdsp.SinSignal(freq=freqsList[count],
                                                amp=1000,
                                                offset=0)
                    encodedWave += signal.make_wave(start=offCount,
                                                    duration=.92)
                else:
                    signal = thinkdsp.SinSignal(freq=freqsList[count],
                                                amp=0,
                                                offset=offCount)
                    encodedWave += signal.make_wave(start=offCount,
                                                    duration=0.92)
                count += 1

            offCount += 1

        encodedWave.play("encodedWave.wav")
        return "encodedWave.wav"
Esempio n. 4
0
def generate_tonal_sig(bits, symbol_len):
    transmit = thinkdsp.SinSignal(freq=500, amp=1.5, offset=0)
    audio = transmit.make_wave(duration=symbol_len)
    for bit in bits:
        if bit == 0:
            sig = thinkdsp.SinSignal(freq=500, amp=1.5, offset=0)
        if bit == 1:
            sig = thinkdsp.SinSignal(freq=1500, amp=1.5, offset=0)
        new_wav = sig.make_wave(duration=symbol_len)
        audio = audio | new_wav
    return audio
Esempio n. 5
0
def window_plot():
    """Makes a plot showing a sinusoid, hamming window, and their product.
    """
    signal = thinkdsp.SinSignal(freq=440)
    duration = signal.period * 10.25
    wave1 = signal.make_wave(duration)
    wave2 = signal.make_wave(duration)

    ys = numpy.hamming(len(wave1.ys))
    window = thinkdsp.Wave(ys, wave1.framerate)

    wave2.hamming()

    thinkplot.preplot(rows=3, cols=1)

    pyplot.subplots_adjust(wspace=0.3,
                           hspace=0.3,
                           right=0.95,
                           left=0.1,
                           top=0.95,
                           bottom=0.05)

    thinkplot.subplot(1)
    wave1.plot()
    thinkplot.config(axis=[0, duration, -1.07, 1.07])

    thinkplot.subplot(2)
    window.plot()
    thinkplot.config(axis=[0, duration, -1.07, 1.07])

    thinkplot.subplot(3)
    wave2.plot()
    thinkplot.config(axis=[0, duration, -1.07, 1.07], xlabel='time (s)')

    thinkplot.save(root='windowing2')
Esempio n. 6
0
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)
Esempio n. 7
0
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')
Esempio n. 8
0
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)
Esempio n. 9
0
def generate_random_wave(complexity):
    f = np.random.randint(1000, size=complexity)
    a = np.random.randint(100, size=complexity)
    rand_wave = 0
    for i in range(complexity):
        sin_sig = thinkdsp.SinSignal(freq=random.randint(300, 2000) * np.random.randn() ** 2,
                                     amp=random.randint(100, 1000) * np.random.randn() ** 2, offset=0)
        rand_wave += sin_sig.make_wave(duration=0.5, start=0, framerate=44100)
    return rand_wave
Esempio n. 10
0
def waveform(freq, btn_input):
    if btn_input == 1:
        signal = thinkdsp.SinSignal(freq)
    if btn_input == 2:
        signal = thinkdsp.SquareSignal(freq)
    if btn_input == 3:
        signal = thinkdsp.TriangleSignal(freq)

    wave = signal.make_wave(framerate=samplingFreq)
    return wave
Esempio n. 11
0
def make_wave(offset):
    """Makes a 440 Hz sine wave with the given phase offset.

    offset: phase offset in radians

    returns: Wave objects
    """
    signal = thinkdsp.SinSignal(freq=440, offset=offset)
    wave = signal.make_wave(duration=0.5, framerate=10000)
    return wave
Esempio n. 12
0
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')
Esempio n. 13
0
def plot_sinusoid(duration=0.00685):
    """Plots three cycles of a 440 Hz sinusoid.

    duration: float
    """
    signal = thinkdsp.SinSignal(440)
    wave = signal.make_wave(duration, framerate=44100)
    wave.plot()
    thinkplot.Save(root='sinusoid1',
                   xlabel='time (s)',
                   axis=[0, duration, -1.05, 1.05])
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()
Esempio n. 15
0
def discontinuity(num_periods=30, hamming=False):
    """Plots the spectrum of a sinusoid with/without windowing.

    num_periods: how many periods to compute
    hamming: boolean whether to apply Hamming window
    """
    signal = thinkdsp.SinSignal(freq=440)
    duration = signal.period * num_periods
    wave = signal.make_wave(duration)

    if hamming:
        wave.hamming()

    print(len(wave.ys), wave.ys[0], wave.ys[-1])
    spectrum = wave.make_spectrum()
    spectrum.plot(high=60)
Esempio n. 16
0
def make_sine():

    framerate = 8000
    signal = thinkdsp.SinSignal(440, amp=1.0, offset=0)
    duration = signal.period
    wave = signal.make_wave(duration=duration, start=0, framerate=framerate)

    print 'Number of samples', len(wave.ys)
    print 'Timestep in ms', 1.0 / framerate * 1000

    # plot the segment
    #wave.plot()
    #thinkplot.Show(
    #    xlabel='time (s)',
    #    axis=[0, duration, -1.05, 1.05])

    print_c_wave(wave)
Esempio n. 17
0
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()
Esempio n. 18
0
def gen_wav():
    #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;
    mix = sin_sig

    wave = mix.make_wave(duration=10, start=0, framerate=2000000)
    wave.normalize() # 归一化
    wave.apodize() # 渐入渐出
    #wave.plot()
    #plt.show()

    segment = wave.segment(start=0, duration=2/440)
    segment.plot()
    plt.show()

    # 频谱
    spectrum = wave.make_spectrum()
    spectrum.plot()
    plt.show()
    wave.write(filename=sys.argv[1])
Esempio n. 19
0
spectrumGuitar = waveguitar.make_spectrum()
spectrumGuitar.plot()

spectrumSax = wavesax.make_spectrum()
spectrumSax.plot(high=6000)

spectrumGuitar = waveguitar.make_spectrum()
spectrumGuitar.plot(high=1000)

spectrumSax.peaks()[:10]
spectrumGuitar.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)')

sin_sig_sax = thinkdsp.SinSignal(freq=492, amp=1.0, offset=0)
sin_sig_guitar = thinkdsp.SinSignal(freq=401, amp=1.0, offset=0)
sin_sig = sin_sig_sax + sin_sig_guitar
sin_sig.plot()
thinkplot.config(xlabel='Time (s)')

wave2 = cos_sig.make_wave(duration=1)
wave2.apodize()
wave2.make_audio()

wave3 = sin_sig.make_wave(duration=1)
wave3.apodize()
wave3.make_audio()
Esempio n. 20
0
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()
Esempio n. 21
0
import thinkdsp
import matplotlib.pyplot as pyplot

pyplot.gcf().clear()

cos_sig = thinkdsp.SinSignal(440, 1, 0)
sin_sig = thinkdsp.SinSignal(880, 1, 0)

mix = cos_sig + sin_sig
wave = mix.make_wave(100, 0, 11025)
print(wave.ys[0])
print(wave.ys[1])
print(wave.ys[2])
print(wave.ys[3])
print(wave.ys[4])
print(wave.ys[5])
print(wave.ys[6])
print(wave.ys[7])
wave.plot()
pyplot.show()
period = mix.period
segment = wave.segment(0, 3 * period)
segment.plot()
Esempio n. 22
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug  2 19:37:36 2018

@author: MikeNagler
"""

import thinkdsp 
import numpy as np 

fundamental = 110
freqs = np.arange(110, 1100, 110) 
amps = 1/freqs**2

mix = sum(thinkdsp.SinSignal(freq=f, amp=a) for f, a in zip(freqs, amps)) 

wave = mix.make_wave()
segment = wave.segment(duration=0.01)

spectrum = wave.make_spectrum() 
spectrum.plot()

wave.write(filename='temp.wav') 
thinkdsp.play_wave(filename='temp.wav', player='afplay')
    
    

Esempio n. 23
0
def make_sine(offset):
    signal = thinkdsp.SinSignal(freq=440, offset=offset)
    wave = signal.make_wave(duration=0.5, framerate=10000)
    return wave
Esempio n. 24
0
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()
# This file shows how to generate a sine wave using the ThinkDSP library.
import sys
sys.path.insert(0, 'ThinkDSP/code/') 
import thinkdsp
import matplotlib.pyplot as pyplot

# Generate wave
sin = thinkdsp.SinSignal(freq=400, amp=0.5) # Where freq is the frequency and amp is the amplitude of the sine wave
wave = sin.make_wave(duration=2, start=0, framerate=44100) # Turn the sin wave into an audio wave

# Play wave
wave.play()

# Plot wave
period = sin.period
segment = wave.segment(start=0, duration=period*3)
segment.plot()
pyplot.show()

Esempio n. 26
0
# ### Exercise
#
# Synthesize a compound signal by creating SinSignal and CosSignal
# objects and adding them up.  Evaluate the signal to get a Wave,
# and listen to it.  Compute its Spectrum and plot it.
# What happens if you add frequency
# components that are not multiples of the fundamental?

# ### Solution
#
# Here are some arbitrary components I chose.  It makes an interesting waveform!

# In[15]:

signal = (thinkdsp.SinSignal(freq=400, amp=1.0) +
          thinkdsp.SinSignal(freq=600, amp=0.5) +
          thinkdsp.SinSignal(freq=800, amp=0.25))
signal.plot()

# We can use the signal to make a wave:

# In[16]:

wave2 = signal.make_wave(duration=1)
wave2.apodize()

# And here's what it sounds like:

# In[17]:
Esempio n. 27
0
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()
Esempio n. 28
0
# ## solution 3

# In[30]:


import thinkdsp as d
from IPython.display import Audio
import thinkplot as t
from IPython.display import Audio


# In[31]:



m=d.SinSignal(freq=523,amp=3,offset= 0)
n=d.SinSignal(freq=554,amp=3,offset=0)
o=d.SinSignal(freq=587,amp=4,offset=0)
p=d.SinSignal(freq=622,amp=5,offset=0)
q=d.SinSignal(freq=659,amp=5,offset=0)
r=d.SinSignal(freq=698,amp=5,offset=0)
s=d.SinSignal(freq=740,amp=5,offset=0)
t=d.SinSignal(freq=780,amp=5,offset=0)
u=d.SinSignal(freq=831,amp=5,offset=0)
v=d.SinSignal(freq=880,amp=5,offset=0)
w=d.SinSignal(freq=932,amp=5,offset=0)
x=d.SinSignal(freq=988,amp=5,offset=0)
y=d.SinSignal(freq=1047,amp=5,offset=0)


# In[34]:
Esempio n. 29
0
import sys
sys.path.insert(0, 'ThinkDSP/code/')
import thinkdsp
import matplotlib.pyplot as pyplot

# Generate wave
sin1 = thinkdsp.SinSignal(freq=400, amp=0.5)
sin2 = thinkdsp.SinSignal(freq=800, amp=0.3)
mix = sin1 + sin2  # Notice here how 2 waves can be added together
wave = mix.make_wave(duration=2, start=0, framerate=44100)

# Play wave
wave.play()

# Plot spectrum of wave
spectrum = wave.make_spectrum()
spectrum.plot()
pyplot.show()
Esempio n. 30
0
t3 = [np.sin(7*np.pi*t)*00 for t in np.linspace(0,4, 1000)]
S=[i+J+K for i,J,K in zip(t1,t2,t3)]



B=np.fft.fft(S)

plt.plot(abs(B[0]))
plt.plot(B)
plt.show()

"""Create a music of your choice and export the audio in .wav format."""

import thinkdsp as d
from IPython.display import Audio
w1=d.SinSignal(freq=240,amp=5,offset=0)
w2=d.SinSignal(freq=270,amp=5,offset=0)
w3=d.SinSignal(freq=300,amp=5,offset=0)
w4=d.SinSignal(freq=320,amp=5,offset=0)
w5=d.SinSignal(freq=360,amp=5,offset=0)
w6=d.SinSignal(freq=400,amp=5,offset=0)
w7=d.SinSignal(freq=450,amp=5,offset=0)


sa_time=w1.make_wave(duration=1.5,start=0,framerate=11000)
re_time=w2.make_wave(duration=1.5,start=1.5,framerate=11000)
ga_time=w3.make_wave(duration=1.5,start=3,framerate=11000)
ma_time=w4.make_wave(duration=1.5,start=4.5,framerate=11000)
pa_time=w5.make_wave(duration=1.5,start=6,framerate=11000)
dha_time=w6.make_wave(duration=1.5,start=7.5,framerate=11000)
ni_time=w7.make_wave(duration=1.5,start=9,framerate=11000)