Beispiel #1
0
def test_simple_sinusoid():
    window_size = 1024
    t = np.linspace(0, 1, window_size)
    x = np.cos(4 * 2 * np.pi * t)
    window = get_window('hamming', window_size)
    window /= sum(window)
    mag_spectrum, phase_spectrum = dft.from_audio(x, window, window_size)
    x_reconstructed = dft.to_audio(mag_spectrum, phase_spectrum, window_size)

    assert mag_spectrum.argmax() == 4
    assert round(mag_spectrum.max()) == -6
    assert round(mag_spectrum.mean()) == -147
    assert np.allclose(x_reconstructed, x * window)
Beispiel #2
0
# matplotlib without any blocking GUI
import matplotlib as mpl

mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

from smst.utils import audio
from smst.models import dft

(fs, x) = audio.read_wav('../../../sounds/oboe-A4.wav')
w = np.hamming(401)
N = 1024
pin = 5000
x1 = x[pin:pin + w.size]
mX, pX = dft.from_audio(x1, w, N)

plt.figure(1, figsize=(9.5, 7))
plt.subplot(311)
plt.plot(np.arange(pin, pin + w.size) / float(fs), x1, 'b', lw=1.5)
plt.axis([pin / float(fs), (pin + w.size) / float(fs), min(x1), max(x1)])
plt.title('x (oboe-A4.wav), M=401')

plt.subplot(3, 1, 2)
plt.plot(fs * np.arange(mX.size) / float(N), mX, 'r', lw=1.5)
plt.axis([0, 8000, -80, max(mX)])
plt.title('mX; Hamming window, N=1024')

plt.subplot(3, 1, 3)
plt.plot(fs * np.arange(mX.size) / float(N), pX, 'c', lw=1.5)
plt.axis([0, 8000, -12, 15])
Beispiel #3
0
import numpy as np
from scipy.signal import hamming, blackmanharris

from smst.models import dft

M = 255
N = 4096
hM = int(M / 2.0)
fs = 44100
f0 = 5000
A0 = .9
ph = 1.5
t = np.arange(-hM, hM + 1) / float(fs)
x = A0 * np.cos(2 * np.pi * f0 * t + ph)
w = hamming(255)
mX, pX = dft.from_audio(x, w, N)
y = dft.to_audio(mX, pX, M) * sum(w)
freqaxis = fs * np.arange(mX.size) / float(N)
taxis = np.arange(N) / float(fs)

plt.figure(1, figsize=(9, 5))
plt.subplot(3, 2, 1)
plt.plot(freqaxis, mX, 'r', lw=1.5)
plt.axis([0, fs / 2, -110, 0])
plt.title('mW1; Hamming')

plt.subplot(3, 2, 3)
plt.plot(freqaxis, pX, 'c', lw=1.5)
plt.axis([0, fs / 2, min(pX), max(pX)])
plt.title('pW1; Hamming')
Beispiel #4
0
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

from smst.utils import audio, peaks
from smst.models import dft

(fs, x) = audio.read_wav('../../../sounds/sine-440-490.wav')
w = np.hamming(3529)
N = 32768
hN = N / 2
t = -20
pin = 4850
x1 = x[pin:pin + w.size]
mX1, pX1 = dft.from_audio(x1, w, N)
ploc = peaks.find_peaks(mX1, t)
pmag = mX1[ploc]
iploc, ipmag, ipphase = peaks.interpolate_peaks(mX1, pX1, ploc)

plt.figure(1, figsize=(9, 6))
plt.subplot(311)
plt.plot(fs * np.arange(pX1.size) / float(N), pX1, 'c', lw=1.5)
plt.plot(fs * iploc / N,
         ipphase,
         marker='x',
         color='b',
         alpha=1,
         linestyle='',
         markeredgewidth=1.5)
plt.axis([200, 1000, -2, 8])
Beispiel #5
0
# matplotlib without any blocking GUI
import matplotlib as mpl

mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import sawtooth

from smst.models import dft

N = 128
x1 = sawtooth(2 * np.pi * np.arange(-N / 2, N / 2) / float(N))
x2 = sawtooth(2 * np.pi * np.arange(-N / 2 - 2, N / 2 - 2) / float(N))
mX1, pX1 = dft.from_audio(x1, np.ones(N), N)
mX2, pX2 = dft.from_audio(x2, np.ones(N), N)

plt.figure(1, figsize=(9.5, 7))
plt.subplot(321)
plt.title('x1=x[n]')
plt.plot(np.arange(-N / 2, N / 2, 1.0), x1, lw=1.5)
plt.axis([-N / 2, N / 2, -1, 1])

plt.subplot(322)
plt.title('x2=x[n-2]')
plt.plot(np.arange(-N / 2, N / 2, 1.0), x2, lw=1.5)
plt.axis([-N / 2, N / 2, -1, 1])

plt.subplot(323)
plt.title('mX1')
plt.plot(np.arange(0, mX1.size, 1.0), mX1, 'r', lw=1.5)
plt.axis([0, mX1.size, min(mX1), max(mX1)])
Beispiel #6
0
N2 = 1024
smoothf = .1
balancef = .7

M1 = w1.size  # size of analysis window
hM1_1 = int(math.floor((M1 + 1) / 2))  # half analysis window size by rounding
hM1_2 = int(math.floor(M1 / 2))  # half analysis window size by floor
M2 = w2.size  # size of analysis window
hM2_1 = int(math.floor((M2 + 1) / 2))  # half analysis window size by rounding
hM2_2 = int(math.floor(M2 / 2))  # half analysis window size by floor2
loc1 = 14843
loc2 = 9294

x1 = x1[loc1 - hM1_1:loc1 + hM1_2]
x2 = x2[loc2 - hM2_1:loc2 + hM2_2]
mX1, pX1 = dft.from_audio(x1, w1, N1)  # compute dft
mX2, pX2 = dft.from_audio(x2, w2, N2)  # compute dft
# morph
mX2smooth = resample(np.maximum(-200, mX2),
                     mX2.size * smoothf)  # smooth spectrum of second sound
mX2 = resample(mX2smooth, mX2.size)
mY = balancef * mX2 + (1 - balancef) * mX1  # generate output spectrum
# -----synthesis-----
y = dft.to_audio(mY, pX1, M1) * sum(w1)  # overlap-add to generate output sound
mY1, pY1 = dft.from_audio(y, w1, M1)  # overlap-add to generate output sound

plt.figure(1, figsize=(12, 9))
plt.subplot(321)
plt.plot(np.arange(N1) / float(fs), x1 * w1, 'b', lw=1.5)
plt.axis([0, N1 / float(fs), min(x1 * w1), max(x1 * w1)])
plt.title('x1 (orchestra.wav)')
Beispiel #7
0
from smst.utils import audio
from smst.models import dft

(fs, x) = audio.read_wav('../../../sounds/oboe-A4.wav')
M = 512
N = 512
start = .8 * fs
x1 = x[start:start + M]
xw = x1 * np.hamming(M)

plt.figure(1, figsize=(9.5, 6))
plt.subplot(311)
plt.plot(np.arange(start, (start + M), 1.0) / fs, xw, 'b', lw=1.5)
plt.axis([start / fs, (start + M) / fs, min(xw), max(xw)])
plt.title('x (oboe-A4.wav), M = 512')
mX, pX = dft.from_audio(x1, np.hamming(N), N)

plt.subplot(312)
plt.plot((fs / 2.0) * np.arange(mX.size) / float(mX.size), mX, 'r', lw=1.5)
plt.axis([0, fs / 4.0, -85, max(mX)])
plt.title('mX, N = 512')

M = 512
N = 2048
start = .8 * fs
x1 = x[start:start + M]
xw = x1 * np.hamming(M)
mX, pX = dft.from_audio(x1, np.hamming(M), N)

plt.subplot(313)
plt.plot((fs / 2.0) * np.arange(mX.size) / float(mX.size), mX, 'r', lw=1.5)
def main(inputFile=demo_sound_path('piano.wav'),
         window='blackman',
         M=511,
         N=1024,
         time=.2,
         interactive=True,
         plotFile=False):
    """
    inputFile: input sound file (monophonic with sampling rate of 44100)
    window: analysis window type (choice of rectangular, hanning, hamming, blackman, blackmanharris)
    M: analysis window size (odd integer value)
    N: fft size (power of two, bigger or equal than than M)
    time: time  to start analysis (in seconds)
    """

    # read input sound (monophonic with sampling rate of 44100)
    fs, x = audio.read_wav(inputFile)

    # compute analysis window
    w = get_window(window, M)

    # get a fragment of the input sound of size M
    sample = int(time * fs)
    if (sample + M >= x.size
            or sample < 0):  # raise error if time outside of sound
        raise ValueError("Time outside sound boundaries")
    x_frame = x[sample:sample + M]

    # compute the dft of the sound fragment
    mX, pX = dft.from_audio(x_frame, w, N)

    # compute the inverse dft of the spectrum
    y = dft.to_audio(mX, pX, w.size) * sum(w)

    # create figure
    plt.figure(figsize=(12, 9))

    # plot the sound fragment
    plt.subplot(4, 1, 1)
    plt.plot(time + np.arange(M) / float(fs), x_frame)
    plt.axis([time, time + M / float(fs), min(x_frame), max(x_frame)])
    plt.ylabel('amplitude')
    plt.xlabel('time (sec)')
    plt.title('input sound: x')

    # plot the magnitude spectrum
    plt.subplot(4, 1, 2)
    plt.plot(float(fs) * np.arange(mX.size) / float(N), mX, 'r')
    plt.axis([0, fs / 2.0, min(mX), max(mX)])
    plt.title('magnitude spectrum: mX')
    plt.ylabel('amplitude (dB)')
    plt.xlabel('frequency (Hz)')

    # plot the phase spectrum
    plt.subplot(4, 1, 3)
    plt.plot(float(fs) * np.arange(pX.size) / float(N), pX, 'c')
    plt.axis([0, fs / 2.0, min(pX), max(pX)])
    plt.title('phase spectrum: pX')
    plt.ylabel('phase (radians)')
    plt.xlabel('frequency (Hz)')

    # plot the sound resulting from the inverse dft
    plt.subplot(4, 1, 4)
    plt.plot(time + np.arange(M) / float(fs), y)
    plt.axis([time, time + M / float(fs), min(y), max(y)])
    plt.ylabel('amplitude')
    plt.xlabel('time (sec)')
    plt.title('output sound: y')

    plt.tight_layout()

    if interactive:
        plt.show()
    if plotFile:
        plt.savefig('output_plots/%s_dft_model.png' %
                    files.strip_file(inputFile))
Beispiel #9
0
from smst.utils import audio
from smst.models import dft

(fs, x) = audio.read_wav('../../../sounds/orchestra.wav')
N = 2048
start = 1.0 * fs
x1 = x[start:start + N]

plt.figure(1, figsize=(12, 9))
plt.subplot(321)
plt.plot(np.arange(N) / float(fs), x1 * np.hamming(N), 'b', lw=1.5)
plt.axis([0, N / float(fs), min(x1 * np.hamming(N)), max(x1 * np.hamming(N))])
plt.title('x (orchestra.wav)')

mX, pX = dft.from_audio(x1, np.hamming(N), N)
startBin = int(N * 500.0 / fs)
nBins = int(N * 4000.0 / fs)
bandpass = (np.hanning(nBins) * 60.0) - 60
filt = np.zeros(mX.size) - 60
filt[startBin:startBin + nBins] = bandpass
mY = mX + filt

plt.subplot(323)
plt.plot(fs * np.arange(mX.size) / float(mX.size), mX, 'r', lw=1.5, label='mX')
plt.plot(fs * np.arange(mX.size) / float(mX.size),
         filt + max(mX),
         'k',
         lw=1.5,
         label='filter')
plt.legend(prop={'size': 10})
Beispiel #10
0
(fs, x2) = audio.read_wav('../../../sounds/impulse-response.wav')
x1 = x[40000:44096]
N = 4096

plt.figure(1, figsize=(9.5, 7))
plt.subplot(3, 2, 1)
plt.title('x1 (ocean.wav)')
plt.plot(x1, 'b')
plt.axis([0, N, min(x1), max(x1)])

plt.subplot(3, 2, 2)
plt.title('x2 (impulse-response.wav)')
plt.plot(x2, 'b')
plt.axis([0, N, min(x2), max(x2)])

mX1, pX1 = dft.from_audio(x1, np.ones(N), N)
mX1 = mX1 - max(mX1)
plt.subplot(3, 2, 3)
plt.title('X1')
plt.plot(mX1, 'r')
plt.axis([0, N / 2, -70, 0])

mX2, pX2 = dft.from_audio(x2, np.ones(N), N)
mX2 = mX2 - max(mX2)
plt.subplot(3, 2, 4)
plt.title('X2')
plt.plot(mX2, 'r')
plt.axis([0, N / 2, -70, 0])

y = np.convolve(x1, x2)
mY, pY = dft.from_audio(y[0:N], np.ones(N), N)
Beispiel #11
0
    y = fftbuffer * N / 2
    return mX, pX, mY, pY, y


# example call of stochasticModel function
if __name__ == '__main__':
    (fs, x) = audio.read_wav('../../../sounds/ocean.wav')
    w = np.hanning(1024)
    N = 1024
    stocf = .1
    envSize = (N * stocf) // 2
    maxFreq = 10000.0
    lastbin = N * maxFreq / fs
    first = 4000
    last = first + w.size
    mX, pX = dft.from_audio(x[first:last], w, N)
    mXenv = resample(np.maximum(-200, mX), envSize)
    mY = resample(mXenv, N / 2)

    plt.figure(1, figsize=(9, 5))
    plt.plot(float(fs) * np.arange(mX.size) / N, mX, 'r', lw=1.5, label=r'$a$')
    plt.plot(float(fs / 2.0) * np.arange(0, envSize) / envSize,
             mXenv,
             color='k',
             lw=1.5,
             label=r'$\tilde a$')
    plt.plot(float(fs) * np.arange(0, N / 2) / N,
             mY,
             'g',
             lw=1.5,
             label=r'$b$')