def detect_start_end_times(pattern_wav, recording_wav, sr, overlap):
    """Find matches for the start/end pattern within the recorded audio"""

    # Compute the STFT of the recordings
    specgram1 = numpy.array(stft.spectrogram(pattern_wav, overlap=overlap))
    specgram2 = numpy.array(stft.spectrogram(recording_wav, overlap=overlap))

    # Restrict the spectrum to the frequency band occupied by the start/end pattern
    pattern = abs(specgram1[7:16, :])
    recording = abs(specgram2[7:16, :])

    # Search for matches of the pattern in the input recording and return a confidence score
    # for each time position of the input recording
    confidence = match_template(recording, pattern)

    # Search for peaks in the confidence score, and choose the two highest peaks
    # Minimum distance between consecutive peaks is set to 1 second
    peaks = peakutils.indexes(confidence[0],
                              thres=0,
                              min_dist=seconds_to_samples(1, overlap, sr))
    peaks = sorted(peaks, key=lambda p: -confidence[0, p])[:2]

    #TODO: throw errors instead of printing, if necessary
    if len(peaks) < 1:
        print "Could not detect a starting beep!"
    elif len(peaks) < 2:
        print "Could only detect one starting beep!"
    else:
        start, end = sorted(peaks)
        print "Initial beep detected at " + "%.3f" % samples_to_seconds(
            start, overlap, sr) + " seconds."
        print "Final beep detected at " + "%.3f" % samples_to_seconds(
            end, overlap, sr) + " seconds."
    return samples_to_seconds(start, overlap,
                              sr), samples_to_seconds(end, overlap, sr)
def detect_start_end_times(pattern_wav, recording_wav, sr, overlap):
    """Find matches for the start/end pattern within the recorded audio"""

    # Compute the STFT of the recordings
    specgram1 = numpy.array(stft.spectrogram(pattern_wav, overlap=overlap))
    specgram2 = numpy.array(stft.spectrogram(recording_wav, overlap=overlap))

    # Restrict the spectrum to the frequency band occupied by the start/end pattern
    pattern = abs(specgram1[7:16,:])
    recording = abs(specgram2[7:16,:])

    # Search for matches of the pattern in the input recording and return a confidence score
    # for each time position of the input recording
    confidence = match_template(recording, pattern)

    # Search for peaks in the confidence score, and choose the two highest peaks
    # Minimum distance between consecutive peaks is set to 1 second
    peaks = peakutils.indexes(confidence[0], thres=0, min_dist=seconds_to_samples(1, overlap, sr))
    peaks = sorted(peaks, key=lambda p: -confidence[0,p])[:2]

    #TODO: throw errors instead of printing, if necessary
    if len(peaks) < 1:
        print "Could not detect a starting beep!"
    elif len(peaks) < 2:
        print "Could only detect one starting beep!"
    else:
        start, end = sorted(peaks)
        print "Initial beep detected at " + "%.3f" % samples_to_seconds(start, overlap, sr) + " seconds."
        print "Final beep detected at " + "%.3f" % samples_to_seconds(end, overlap, sr) + " seconds."
    return samples_to_seconds(start, overlap, sr), samples_to_seconds(end, overlap, sr)
示例#3
0
def test_shape(length, framelength):
    a = numpy.squeeze(numpy.random.random((length, 1)))

    x = stft.spectrogram(a, framelength=framelength, halved=True)
    assert x.shape[0] == framelength / 2 + 1

    x_2 = stft.spectrogram(a, framelength=framelength, halved=False)
    assert x_2.shape[0] == framelength
示例#4
0
def test_shape(length, framelength):
    a = numpy.squeeze(numpy.random.random((length, 1)))

    x = stft.spectrogram(a, framelength=framelength, halved=True)
    assert x.shape[0] == framelength / 2 + 1

    x_2 = stft.spectrogram(a, framelength=framelength, halved=False)
    assert x_2.shape[0] == framelength
示例#5
0
def test_windowlength_errors():
    """
    Test if way too short signals can be transformed

    """
    siglen = 512
    framelen = 2048

    stft.spectrogram(numpy.random.random(siglen), framelength=framelen)
示例#6
0
def test_maxdim():
    a = numpy.random.random((512, 2, 2))

    with pytest.raises(ValueError):
        stft.spectrogram(a)

    b = numpy.random.random((512, 2, 2, 3))
    with pytest.raises(ValueError):
        stft.ispectrogram(b)
示例#7
0
def test_windowlength_errors():
    """
    Test if way too short signals can be transformed

    """
    siglen = 512
    framelen = 2048

    stft.spectrogram(numpy.random.random(siglen), framelength=framelen)
示例#8
0
def createMatrix():
    # spectrogram_arguments = {'framelength': 512, 'overlap': 512, 'window': scipy.signal.hamming(512)}
    def saveFile(fn, data):
        f = open(fn, 'wb')
        pickle.dump(data, f)
        f.close()

    fs1, data1 = wavfile.read(raw1)
    fs2, data2 = wavfile.read(raw2)

    minlen = min(len(data1), len(data2))
    data1 = data1[:minlen]
    data2 = data2[:minlen]

    spec1 = stft.spectrogram(data1)
    spec2 = stft.spectrogram(data2)

    # Reduce dimension
    spec1 = squeeze(spec1)
    spec2 = squeeze(spec2)

    # same dimensions
    a = np.zeros(spec1.shape)
    b = np.zeros(spec2.shape)

    # hard
    for i in range(len(spec1)):
        for j in range(len(spec1[0])):
            if abs(spec1[i][j]) < abs(spec2[i][j]):
                b[i][j] = 1.0
            else:
                a[i][j] = 1.0

    # soft
    # for i in range(len(spec1)):
    # 	for j in range(len(spec1[0])):
    # 		if (abs(spec1[i][j]) + abs(spec2[i][j])) == 0:
    # 			continue
    # 		a[i][j] = abs(spec1[i][j]) / (abs(spec1[i][j]) + abs(spec2[i][j]))
    # 		b[i][j] = abs(spec2[i][j]) / (abs(spec1[i][j]) + abs(spec2[i][j]))

    fs, data = wavfile.read(merged)
    spec = stft.spectrogram(data)
    spec = squeeze(spec)

    output_a = createSpectrogram(np.multiply(a, spec), spec)
    output_b = createSpectrogram(np.multiply(b, spec), spec)

    output_a2 = stft.ispectrogram(output_a)
    output_b2 = stft.ispectrogram(output_b)

    writeWav(separated_dir + "a.wav", fs1, output_a2)
    writeWav(separated_dir + "b.wav", fs1, output_b2)

    return
示例#9
0
def mdst(
    x,
    odd=True,
    transforms=None,
    **kwargs
):
    """ Calculate lapped MDST of input signal

    Parameters
    ----------
    x : array_like
        The input signal
    odd : boolean, optional
        Switch to oddly stacked transform. Defaults to :code:`True`.
    transforms : module, optional
        Module reference to core transforms. Mostly used to replace
        fast with slow core transforms, for testing. Defaults to
        :mod:`mdct.fast`
        Additional keyword arguments passed to :code:`stft.spectrogram`

    Returns
    -------
    out : array_like
        The output signal

    See Also
    --------
    mdct.fast.transforms.mdst : MDST

    """
    if transforms is None:
        transforms = transforms_default

    kwargs.setdefault('framelength', 2048)

    if not odd:
        return stft.spectrogram(
            x,
            transform=[
                functools.partial(transforms.mdst, odd=False),
                functools.partial(transforms.mdct, odd=False),
            ],
            halved=False,
            **kwargs
        )
    else:
        return stft.spectrogram(
            x,
            transform=transforms.mdst,
            halved=False,
            **kwargs
        )
示例#10
0
def test_maxdim():
    """
    Test if breaking elementary limitations (2D signal, 3D spectrogram at most)
    are caught appropriately

    """
    a = numpy.random.random((512, 2, 2))

    with pytest.raises(ValueError):
        stft.spectrogram(a)

    b = numpy.random.random((512, 2, 2, 3))
    with pytest.raises(ValueError):
        # we cannot infer data from a NumPy array, so we set framelengt here
        stft.ispectrogram(b, framelength=1024)
示例#11
0
def test_maxdim():
    """
    Test if breaking elementary limitations (2D signal, 3D spectrogram at most)
    are caught appropriately

    """
    a = numpy.random.random((512, 2, 2))

    with pytest.raises(ValueError):
        stft.spectrogram(a)

    b = numpy.random.random((512, 2, 2, 3))
    with pytest.raises(ValueError):
        # we cannot infer data from a NumPy array, so we set framelengt here
        stft.ispectrogram(b, framelength=1024)
def voiceMusicSeparation(audio, masktype=1, lamb=1.25, gain=1.25):
    import stft
    # stft
    specgram = stft.spectrogram(audio)
    # rpca
    D = abs(specgram)
    angle = np.angle(specgram)
    A_mag, E_mag, numiter = ialmRPCA(D, lamb)
    A = A_mag * scipy.exp(angle * 1j)
    E = E_mag * scipy.exp(angle * 1j)
    # binary mask
    if (masktype):
        m = 1.0 * (abs(E_mag) > abs(gain * A_mag))
        Emask = m * specgram
        Amask = specgram - Emask
    else:
        Emask = E
        Amask = A
    # istft
    outputA = stft.ispectrogram(Amask)
    outputE = stft.ispectrogram(Emask)
    #output
    wavoutA = np.array(outputA[:len(audio)], dtype=np.int16)
    wavoutE = np.array(outputE[:len(audio)], dtype=np.int16)
    return wavoutA, wavoutE
示例#13
0
def cmdct(x, odd=True, transforms=None, **kwargs):
    """ Calculate lapped complex MDCT/MCLT of input signal

    Parameters
    ----------
    x : array_like
        The input signal
    odd : boolean, optional
        Switch to oddly stacked transform. Defaults to :code:`True`.
    transforms : module, optional
        Module reference to core transforms. Mostly used to replace
        fast with slow core transforms, for testing. Defaults to
        :mod:`mdct.fast`
    **kwargs, optional
        Additional keyword arguments passed to :code:`stft.spectrogram`

    Returns
    -------
    out : array_like
        The output signal

    See Also
    --------
    mdct.fast.transforms.cmdct : complex MDCT

    """
    if transforms is None:
        transforms = transforms_default

    return stft.spectrogram(x,
                            transform=functools.partial(transforms.cmdct,
                                                        odd=odd),
                            halved=False,
                            **kwargs)
示例#14
0
def plot_spec(f):
    a = read(f)
    spec = stft.spectrogram(a[1])
    auto = np.abs(spec * np.conj(spec))
    norm = normalize(auto, norm="l2")
    img = plt.imshow(norm, origin='lower', cmap='jet', interpolation='nearest', aspect='auto')
    plt.show()
示例#15
0
def test_issue_autoinverse_defaults(signal):
    """
    Using defaults in inverse did not work because there were none in place

    """
    x = numpy.array(stft.spectrogram(signal))
    y = stft.ispectrogram(x)
示例#16
0
def calc_stft(s_):
    # s_time = begin_rec + timedelta(seconds=int(i*numts))
    s = s_.transpose()

    stft_data = stft.spectrogram(s, framelength=250, centered=False)
    stft_data = np.transpose(stft_data, (1, 2, 0))
    stft_data = np.abs(stft_data) + 1e-6

    # if self.settings['dataset'] == 'FB':
    #     stft_data = np.concatenate((stft_data[:,:,1:47],
    #                                 stft_data[:,:,54:97],
    #                                 stft_data[:,:,104:]),
    #                                 axis=-1)
    # elif self.settings['dataset'] == 'CHBMIT':
    #     stft_data = np.concatenate((stft_data[:,:,1:57],
    #                                 stft_data[:,:,64:117],
    #                                 stft_data[:,:,124:]),
    #                                 axis=-1)
    # elif self.settings['dataset'] == 'EpilepsiaSurf':
    stft_data = stft_data[:,:,1:]
    stft_data = np.log10(stft_data)
    indices = np.where(stft_data <= 0)
    stft_data[indices] = 0

    # from matplotlib import cm
    # from matplotlib import pyplot as plt
    # plt.matshow(stft_data[0]/np.max(stft_data[0]))
    # plt.colorbar()
    # plt.show()

    stft_data = stft_data.reshape(-1, stft_data.shape[0],
                                  stft_data.shape[1],
                                  stft_data.shape[2])

    return stft_data
示例#17
0
def test_issue_autoinverse_defaults(signal):
    """
    Using defaults in inverse did not work because there were none in place

    """
    x = numpy.array(stft.spectrogram(signal))
    y = stft.ispectrogram(x)
 def getSTFT(self, framesize=512, hopsize=256, window=signal.hann):
     espectograma = np.transpose(stft.spectrogram(self.data,
                                                  framelength=framesize,
                                                  hopsize=hopsize,
                                                  centered=False,
                                                  window=window,
                                                  halved=True))
     return espectograma
示例#19
0
def test_issue_autoinverse_values(signal, framelength):
    """
    Passing values to inverse on a plain array failed as the values were
    not actually used

    """
    x = numpy.array(stft.spectrogram(signal, framelength=framelength))
    y = stft.ispectrogram(x, framelength=framelength)
示例#20
0
def test_issue_autoinverse_values(signal, framelength):
    """
    Passing values to inverse on a plain array failed as the values were
    not actually used

    """
    x = numpy.array(stft.spectrogram(signal, framelength=framelength))
    y = stft.ispectrogram(x, framelength=framelength)
示例#21
0
def mdst(x, odd=True, transforms=None, **kwargs):
    """ Calculate lapped MDST of input signal

    Parameters
    ----------
    x : array_like
        The input signal
    odd : boolean, optional
        Switch to oddly stacked transform. Defaults to :code:`True`.
    transforms : module, optional
        Module reference to core transforms. Mostly used to replace
        fast with slow core transforms, for testing. Defaults to
        :mod:`mdct.fast`
        Additional keyword arguments passed to :code:`stft.spectrogram`

    Returns
    -------
    out : array_like
        The output signal

    See Also
    --------
    mdct.fast.transforms.mdst : MDST

    """
    if transforms is None:
        transforms = transforms_default

    kwargs.setdefault('framelength', 2048)

    if not odd:
        return stft.spectrogram(x,
                                transform=[
                                    functools.partial(transforms.mdst,
                                                      odd=False),
                                    functools.partial(transforms.mdct,
                                                      odd=False),
                                ],
                                halved=False,
                                **kwargs)
    else:
        return stft.spectrogram(x,
                                transform=transforms.mdst,
                                halved=False,
                                **kwargs)
def wav_to_magnitude_phase(wav, window_size):
    fourier = spectrogram(wav, framelength=window_size)
    magnitude = np.abs(fourier)
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        phase = fourier / magnitude
    phase[~np.isfinite(phase)] = 1.0  # when magnitude is zero, set phase to 1
    dtype = wav.dtype
    return magnitude, phase, dtype
示例#23
0
def _extract_stft(audios):
    stfts = []
    for ad in audios:
        st = stft.spectrogram(ad,
                              framelength=config.STFT_POINT,
                              overlap=config.STFT_OVERLAP)
        st = st.transpose()
        stfts.append(st)
    return stfts
def wav_to_spectrogram(filename):
    # 8000 Hz sampling
    # framelength of 128 gives
    # 0-4000 Hz range in spectral bins
    fs, audio = wav.read(filename)
    specgram = stft.spectrogram(audio, framelength=framelength)
    specgramabs = numpy.abs(specgram).transpose()
    #print specgramabs.shape
    return specgramabs
示例#25
0
def plot_pca_spec(f):
    pca = PCA(n_components=200, copy=False, whiten=False)
    a = read(f)
    spec = stft.spectrogram(a[1])
    auto = np.abs(spec * np.conj(spec))
    norm = normalize(auto, norm="l2")
    w = 20 * np.log10(pca.fit_transform(norm))
    clipped = np.clip(w, -40, 200)
    img = plt.imshow(clipped, origin='lower', cmap='jet', interpolation='nearest', aspect='auto')
    plt.show()
示例#26
0
def test_multiple_transforms(signal):
    """
    Test if giving multiple different transforms works OK

    """
    a = signal

    x = stft.spectrogram(a, transform=[scipy.fftpack.fft, numpy.fft.fft])
    y = stft.ispectrogram(x, transform=[scipy.fftpack.ifft, numpy.fft.ifft])

    assert numpy.allclose(a, y)
示例#27
0
def test_real(signal):
    """
    Test if real valued input results in real valued output

    """
    a = signal

    x = stft.spectrogram(a)
    y = stft.ispectrogram(x)

    assert y.dtype == numpy.float64
示例#28
0
def test_issue1():
    """
    Passing a (x, 1) shape signal created a 3D tensor output, while
    a (x,) shape signal created a 2D matrix. This should not happen.

    """
    a = numpy.random.random((512, 1))

    b = stft.spectrogram(a)

    assert b.ndim == 2
示例#29
0
def plot_pca_spec2(f):
    pca = PCA(n_components=40, copy=False, whiten=True)
    a = read(f)
    spec = stft.spectrogram(a[1])
    v = spec.real.astype(float)
    auto = np.abs(v * np.conj(v))
    norm = normalize(auto, norm="l2")
    w = (pca.fit_transform(norm))
    # clipped = np.clip(w, -40, 200)
    img = plt.imshow(norm, origin='lower', cmap='jet', interpolation='nearest', aspect='auto')
    plt.show()
示例#30
0
def test_issue1():
    """
    Passing a (x, 1) shape signal created a 3D tensor output, while
    a (x,) shape signal created a 2D matrix. This should not happen.

    """
    a = numpy.random.random((512, 1))

    b = stft.spectrogram(a)

    assert b.ndim == 2
示例#31
0
def wav_to_json(wav_list, json_list):
    pca = PCA(n_components=40, copy=False, whiten=True)
    for f in file_list:
        a = read(f)
        spec = stft.spectrogram(a[1])
        v = spec.real.astype(float)
        norm = normalize(v, norm="l2")
        whitened = pca.fit_transform(norm)
        temp = pd.DataFrame(whitened)
        with open(json_list[0], 'w') as myfile:
            json.dumps(temp.to_json(path_or_buf=json_list[0]), myfile)
示例#32
0
def wav_to_json(wav_list, json_list):
    pca = PCA(n_components=40, copy=False, whiten=True)
    for f in file_list:
        a = read(f)
        spec = stft.spectrogram(a[1])
        v = spec.real.astype(float)
        norm = normalize(v, norm="l2")
        whitened = pca.fit_transform(norm)
        temp = pd.DataFrame(whitened)
        with open(json_list[0], 'w') as myfile:
            json.dumps(temp.to_json(path_or_buf=json_list[0]), myfile)
示例#33
0
def plot_spec(f):
    a = read(f)
    spec = stft.spectrogram(a[1])
    auto = np.abs(spec * np.conj(spec))
    norm = normalize(auto, norm="l2")
    img = plt.imshow(norm,
                     origin='lower',
                     cmap='jet',
                     interpolation='nearest',
                     aspect='auto')
    plt.show()
示例#34
0
def test_multiple_transforms(signal):
    """
    Test if giving multiple different transforms works OK

    """
    a = signal

    x = stft.spectrogram(a, transform=[scipy.fftpack.fft, numpy.fft.fft])
    y = stft.ispectrogram(x, transform=[scipy.fftpack.ifft, numpy.fft.ifft])

    assert numpy.allclose(a, y)
示例#35
0
def test_overriding(channels, padding, signal, framelength):
    """
    Test if overriding transform settings works

    """
    a = signal

    x = stft.spectrogram(a, framelength=framelength, padding=padding)
    y = stft.ispectrogram(x, framelength=framelength)

    # We were using no overlap during inverse, so our output is twice as long
    assert numpy.allclose(a, y)
示例#36
0
def test_precision(channels, padding, signal, framelength):
    """
    Test if transform-inverse identity holds

    """
    a = signal

    x = stft.spectrogram(a, framelength=framelength, padding=padding)
    y = stft.ispectrogram(x, framelength=framelength, padding=padding)

    # Crop first and last frame
    assert numpy.allclose(a, y)
示例#37
0
def test_rms(channels, padding, signal, framelength):
    """
    Test if transform-inverse identity holds

    """
    a = signal

    x = stft.spectrogram(a, framelength=framelength, padding=padding)
    y = stft.ispectrogram(x, framelength=framelength, padding=padding)

    # Crop first and last frame
    assert numpy.sqrt(numpy.mean((a - y) ** 2)) < 1e-8
示例#38
0
def spectral_centroid(wavedata,window_size,sample_rate):
    magnitude_spectrum=stft.spectrogram(wavedata,window_size)
    timebins,freqbins=np.shape(magnitude_spectrum)
    timestamps=(np.arange(0,timebins-1)*(timebins/float(sample_rate)))
    sc=[]
    for t in range(timebins-1):
        power_spectrum=np.abs(magnitude_spectrum[t])**2
        sc_t=np.sum(power_spectrum*np.arange(1,freqbins+1))/np.sum(power_spectrum)
        sc.append(sc_t)
    sc=np.asarray(sc)
    sc=np.nan_to_num(sc)
    return sc, np.asarray(timestamps)
示例#39
0
def test_overriding(channels, padding, signal, framelength):
    """
    Test if overriding transform settings works

    """
    a = signal

    x = stft.spectrogram(a, framelength=framelength, padding=padding)
    y = stft.ispectrogram(x, framelength=framelength)

    # We were using no overlap during inverse, so our output is twice as long
    assert numpy.allclose(a, y)
示例#40
0
def test_precision(channels, padding, signal, framelength, halved):
    """
    Test if transform-inverse identity holds

    """
    a = signal

    x = stft.spectrogram(
        a, framelength=framelength, padding=padding, halved=halved
    )
    y = stft.ispectrogram(x)

    assert numpy.allclose(a, y)
示例#41
0
def test_rms(channels, padding, signal, framelength, halved):
    """
    Test if transform-inverse identity holds

    """
    a = signal

    x = stft.spectrogram(
        a, framelength=framelength, padding=padding, halved=halved
    )
    y = stft.ispectrogram(x)

    assert numpy.sqrt(numpy.mean((a - y) ** 2)) < 1e-8
示例#42
0
def wav_to_df(input_file):
    whitened = {}
    df = pd.DataFrame()
    pca = PCA(n_components=40, copy=False, whiten=True)
    a = read(input_file)
    spec = stft.spectrogram(a[1])
    autopower = np.abs(spec * np.conj(spec))
    v = autopower.real.astype(float)
    norm = normalize(v, norm="l2")
    whitened[input_file] = 20 * np.log10(pca.fit_transform(norm))
    temp = pd.DataFrame(whitened.items(), columns=['Filename', 'Spec_features'])
    df = df.append(temp)
    return df, whitened
示例#43
0
def spectral_rolloff(wavedata,window_size,sample_rate,k=0.85):
    magnitude_spectrum=stft.spectrogram(wavedata,window_size)
    power_spectrum=np.abs(magnitude_spectrum)**2
    timebins,freqbins=np.shape(magnitude_spectrum)
    timestamps=(np.arange(0,timebins-1)*(timebins/float(sample_rate)))
    sr=[]
    spectralSum=np.sum(power_spectrum,axis=1)
    for t in range(timebins-1):
        sr_t=np.where(np.cumsum(power_spectrum[t,:])>= k*spectralSum[t])[0][0]
        sr.append(sr_t)
    sr=np.asarray(sr).astype(float)
    sr=(sr/freqbins)*(sample_rate/2.0)
    return sr,np.asarray(timestamps)
示例#44
0
def test_rms(channels, padding, signal, framelength, halved):
    """
    Test if transform-inverse identity holds

    """
    a = signal

    x = stft.spectrogram(a,
                         framelength=framelength,
                         padding=padding,
                         halved=halved)
    y = stft.ispectrogram(x)

    assert numpy.sqrt(numpy.mean((a - y)**2)) < 1e-8
示例#45
0
def wav_to_df(input_file):
    whitened = {}
    df = pd.DataFrame()
    pca = PCA(n_components=40, copy=False, whiten=True)
    a = read(input_file)
    spec = stft.spectrogram(a[1])
    autopower = np.abs(spec * np.conj(spec))
    v = autopower.real.astype(float)
    norm = normalize(v, norm="l2")
    whitened[input_file] = 20 * np.log10(pca.fit_transform(norm))
    temp = pd.DataFrame(whitened.items(),
                        columns=['Filename', 'Spec_features'])
    df = df.append(temp)
    return df, whitened
示例#46
0
def test_complex(signal):
    """
    Test transform-inverse works for complex input

    """
    a = signal

    # create complex test vectors by adding random phase
    c = a + 1j*numpy.random.random(a.shape)
    x = stft.spectrogram(c, halved=False)
    y = stft.ispectrogram(x, halved=False)

    assert c.dtype == y.dtype
    assert numpy.allclose(c, y)
示例#47
0
def test_precision(channels, padding, signal, framelength, halved):
    """
    Test if transform-inverse identity holds

    """
    a = signal

    x = stft.spectrogram(a,
                         framelength=framelength,
                         padding=padding,
                         halved=halved)
    y = stft.ispectrogram(x)

    assert numpy.allclose(a, y)
示例#48
0
def plot_pca_spec(f):
    pca = PCA(n_components=200, copy=False, whiten=False)
    a = read(f)
    spec = stft.spectrogram(a[1])
    auto = np.abs(spec * np.conj(spec))
    norm = normalize(auto, norm="l2")
    w = 20 * np.log10(pca.fit_transform(norm))
    clipped = np.clip(w, -40, 200)
    img = plt.imshow(clipped,
                     origin='lower',
                     cmap='jet',
                     interpolation='nearest',
                     aspect='auto')
    plt.show()
示例#49
0
def plot_pca_spec2(f):
    pca = PCA(n_components=40, copy=False, whiten=True)
    a = read(f)
    spec = stft.spectrogram(a[1])
    v = spec.real.astype(float)
    auto = np.abs(v * np.conj(v))
    norm = normalize(auto, norm="l2")
    w = (pca.fit_transform(norm))
    # clipped = np.clip(w, -40, 200)
    img = plt.imshow(norm,
                     origin='lower',
                     cmap='jet',
                     interpolation='nearest',
                     aspect='auto')
    plt.show()
def make_spec(file_list):
    '''
    INPUT:
        list of wav file - files will be converted to mono in function
    OUTPUT:
        dictionary with filename as key, spectrogram as value
    '''
    spectrograms = {}
    for f in file_list:
        sound = AudioSegment.from_wav(f)
        sound = sound.set_channels(1)
        sound.export("temp", format="wav")
        a = read("temp")
        # arr = np.array(a[1], dtype=float)  already np array - don't need to convert
        spec = stft.spectrogram(a[1])
        spectrograms[f] = spec
    return spectrograms
def make_spec(file_list):
    '''
    INPUT:
        list of wav file - files will be converted to mono in function
    OUTPUT:
        dictionary with filename as key, spectrogram as value
    '''
    spectrograms = {}
    for f in file_list:
        sound = AudioSegment.from_wav(f)
        sound = sound.set_channels(1)
        sound.export("temp", format="wav")
        a = read("temp")
        # arr = np.array(a[1], dtype=float)  already np array - don't need to convert
        spec = stft.spectrogram(a[1])
        spectrograms[f] = spec
    return spectrograms
def convert():
    for item in dirs:
        if os.path.isfile(path + item):
            print(path + item)
            x = np.loadtxt(path + item,
                           delimiter=',',
                           unpack=True,
                           dtype='float32')
            f, e = os.path.splitext(path + item)

            z_data = np.transpose(x[2])
            # specgram_z = stft.spectrogram(z_data)
            specgram_z = stft.spectrogram(z_data, window=0.4)
            plt._imsave(f + '.jpg',
                        abs(specgram_z),
                        vmin=-40,
                        vmax=40,
                        cmap=plt.get_cmap('coolwarm'),
                        format='jpg')  # gray Wistia
示例#53
0
def divide():
    def loadFile(fn):
        f = open(fn, 'rb')
        data = pickle.load(f)
        f.close()
        return data

    fs, data = wavfile.read(merged)
    spec = stft.spectrogram(data, framelength=512)
    spec = squeeze(spec)
    Ma = loadFile(m_dir + "M_" + raw1[:-4])
    Mb = loadFile(m_dir + "M_" + raw2[:-4])
    a = createSpectrogram(np.dot(Ma, spec), spec)
    b = createSpectrogram(np.dot(Mb, spec), spec)

    output_a = stft.ispectrogram(a)
    output_b = stft.ispectrogram(b)

    writeWav(separated_dir + "a.wav", fs, output_a)
    writeWav(separated_dir + "b.wav", fs, output_b)
示例#54
0
 def stft_audio(self, path, file, nlength = 1024, olap = 4, librosamode = 1, FS=16000):
     """
     http://stft.readthedocs.io/en/latest/index.html
     Receive specific folder and file to extract the STFT
     All audio are resample to 16 kHz if FS is not specified
     """
     fs, s = wav.read('%s/%s' % (path, file))
     dim = len(s.shape)
     if (dim>1):
         s = s[:, 0]
     if (fs != FS):
         n_s = round(len(s) * (FS / fs))
         s = signal.resample(s, n_s)
     if librosamode == 0:
         specgram = stft.spectrogram(s, framelength=nlength, overlap=olap)
     else:
         #S = librosa.stft(s, n_fft=nlength, hop_length= olap)
         S = librosa.stft(s)
         specgram = librosa.logamplitude(np.abs(S))
     return specgram.T
示例#55
0
def cmdct(
    x,
    odd=True,
    transforms=None,
    **kwargs
):
    """ Calculate lapped complex MDCT/MCLT of input signal

    Parameters
    ----------
    x : array_like
        The input signal
    odd : boolean, optional
        Switch to oddly stacked transform. Defaults to :code:`True`.
    transforms : module, optional
        Module reference to core transforms. Mostly used to replace
        fast with slow core transforms, for testing. Defaults to
        :mod:`mdct.fast`
    **kwargs, optional
        Additional keyword arguments passed to :code:`stft.spectrogram`

    Returns
    -------
    out : array_like
        The output signal

    See Also
    --------
    mdct.fast.transforms.cmdct : complex MDCT

    """
    if transforms is None:
        transforms = transforms_default

    return stft.spectrogram(
        x,
        transform=functools.partial(transforms.cmdct, odd=odd),
        halved=False,
        **kwargs
    )
示例#56
0
    for i, clean in enumerate(os.listdir(INPUT_CLEAN_DIR)):

        if clean[-4:] == '.wav':
            rate_clean, data_clean = wavfile.read(INPUT_CLEAN_DIR + clean)
            for noise in noise_data:
                data_noise = noise[:]

                length = len(data_clean)
                data_noise = data_noise[:length][:]

                data_combined = np.array([
                    (s1 / 2 + s2 / 2)
                    for (s1, s2) in zip(data_clean, data_noise)
                ])

                Sx_clean = stft.spectrogram(data_clean).transpose() / 100000
                Sx_noise = stft.spectrogram(data_noise).transpose() / 100000
                Sx_combined = stft.spectrogram(
                    data_combined).transpose() / 100000
                # Sx_clean = pretty_spectrogram(data_clean.astype('float64'), fft_size=fft_size, step_size=step_size, thresh=spec_thresh)
                # Sx_noise = pretty_spectrogram(data_noise.astype('float64'), fft_size=fft_size, step_size=step_size, thresh=spec_thresh)
                # Sx_combined = pretty_spectrogram(data_combined.astype('float64'), fft_size=fft_size, step_size=step_size, thresh=spec_thresh)

                # Sx_target = np.concatenate((Sx_clean, Sx_noise), axis=0)
                # print(clean)
                # print (Sx_clean.shape)

                processed_data.append([Sx_combined, Sx_clean, Sx_noise])

            curr_batch += 1
            if curr_batch == batch_size:
示例#57
0
def mdct(
    x,
    odd=True,
    transforms=None,
    **kwargs
):
    """ Calculate lapped MDCT of input signal

    Parameters
    ----------
    x : array_like
        The signal to be transformed. May be a 1D vector for single channel or
        a 2D matrix for multi channel data. In case of a mono signal, the data
        is must be a 1D vector of length :code:`samples`. In case of a multi
        channel signal, the data must be in the shape of :code:`samples x
        channels`.
    odd : boolean, optional
        Switch to oddly stacked transform. Defaults to :code:`True`.
    framelength : int
        The signal frame length. Defaults to :code:`2048`.
    hopsize : int
        The signal frame hopsize. Defaults to :code:`None`. Setting this
        value will override :code:`overlap`.
    overlap : int
        The signal frame overlap coefficient. Value :code:`x` means
        :code:`1/x` overlap. Defaults to :code:`2`. Note that anything but
        :code:`2` will result in a filterbank without perfect reconstruction.
    centered : boolean
        Pad input signal so that the first and last window are centered around
        the beginning of the signal. Defaults to :code:`True`.
        Disabling this will result in aliasing
        in the first and last half-frame.
    window : callable, array_like
        Window to be used for deringing. Can be :code:`False` to disable
        windowing. Defaults to :code:`scipy.signal.cosine`.
    transforms : module, optional
        Module reference to core transforms. Mostly used to replace
        fast with slow core transforms, for testing. Defaults to
        :mod:`mdct.fast`
    padding : int
        Zero-pad signal with x times the number of samples.
        Defaults to :code:`0`.
    save_settings : boolean
        Save settings used here in attribute :code:`out.stft_settings` so that
        :func:`ispectrogram` can infer these settings without the developer
        having to pass them again.

    Returns
    -------
    out : array_like
        The signal (or matrix of signals). In case of a mono output signal, the
        data is formatted as a 1D vector of length :code:`samples`. In case of
        a multi channel output signal, the data is formatted as :code:`samples
        x channels`.

    See Also
    --------
    mdct.fast.transforms.mdct : MDCT

    """
    if transforms is None:
        transforms = transforms_default

    kwargs.setdefault('framelength', 2048)

    if not odd:
        return stft.spectrogram(
            x,
            transform=[
                functools.partial(transforms.mdct, odd=False),
                functools.partial(transforms.mdst, odd=False),
            ],
            halved=False,
            **kwargs
        )
    else:
        return stft.spectrogram(
            x,
            transform=transforms.mdct,
            halved=False,
            **kwargs
        )
        def process_raw_data(mat_data):
            print('Loading data')
            #print mat_data
            X = []
            y = []

            #scale_ = scale_coef[target]

            for data in mat_data:
                # CHBMIT: select 16 most significant only,
                # to have all pats have same no. channels
                # if self.settings['dataset'] == 'CHBMIT':
                #     print ('Channel selection for CHBMIT')
                #     print (data.shape)
                #     data = data[:,self.significant_channels]
                #     print (data.shape)

                if ictal:
                    y_value = 1
                    first_segment = False
                else:
                    y_value = 0

                X_temp = []
                y_temp = []

                totalSample = int(data.shape[0] / targetFrequency / numts) + 1
                window_len = int(targetFrequency * numts)
                for i in range(totalSample):
                    if (i + 1) * window_len <= data.shape[0]:
                        s = data[i * window_len:(i + 1) * window_len, :]

                        stft_data = stft.spectrogram(
                            s, framelength=targetFrequency, centered=False)
                        stft_data = np.abs(stft_data) + 1e-6
                        stft_data = np.log10(stft_data)
                        indices = np.where(stft_data <= 0)
                        stft_data[indices] = 0

                        stft_data = np.transpose(stft_data, (2, 1, 0))
                        if self.settings['dataset'] == 'FB':
                            stft_data = np.concatenate(
                                (stft_data[:, :, 1:47], stft_data[:, :, 54:97],
                                 stft_data[:, :, 104:]),
                                axis=-1)
                        elif self.settings['dataset'] == 'CHBMIT':
                            stft_data = np.concatenate(
                                (stft_data[:, :, 1:57],
                                 stft_data[:, :, 64:117], stft_data[:, :,
                                                                    124:]),
                                axis=-1)
                        elif self.settings['dataset'] == 'EpilepsiaSurf':
                            stft_data = stft_data[:, :, 1:]

                        proj = np.sum(stft_data, axis=(0, 1), keepdims=False)
                        self.global_proj += proj / 1000.0
                        '''
                        from matplotlib import cm
                        plt.matshow(stft_data[0]/np.max(stft_data[0]))
                        plt.colorbar()
                        plt.show()
                        '''
                        #stft_data = np.multiply(stft_data,1.0/scale_)
                        '''
                        plt.matshow(stft_data[0]/np.max(stft_data[0]))
                        plt.colorbar()
                        plt.show()
                        '''

                        stft_data = stft_data.reshape(-1, stft_data.shape[0],
                                                      stft_data.shape[1],
                                                      stft_data.shape[2])

                        X_temp.append(stft_data)
                        y_temp.append(y_value)

                #overlapped window
                if ictal:
                    i = 1
                    print('ictal_ovl_len =', ictal_ovl_len)
                    while (window_len +
                           (i + 1) * ictal_ovl_len <= data.shape[0]):
                        s = data[i * ictal_ovl_len:i * ictal_ovl_len +
                                 window_len, :]

                        stft_data = stft.spectrogram(
                            s, framelength=targetFrequency, centered=False)
                        stft_data = np.abs(stft_data) + 1e-6
                        stft_data = np.log10(stft_data)
                        indices = np.where(stft_data <= 0)
                        stft_data[indices] = 0

                        stft_data = np.transpose(stft_data, (2, 1, 0))
                        if self.settings['dataset'] == 'FB':
                            stft_data = np.concatenate(
                                (stft_data[:, :, 1:47], stft_data[:, :, 54:97],
                                 stft_data[:, :, 104:]),
                                axis=-1)
                        elif self.settings['dataset'] == 'CHBMIT':
                            stft_data = np.concatenate(
                                (stft_data[:, :, 1:57],
                                 stft_data[:, :, 64:117], stft_data[:, :,
                                                                    124:]),
                                axis=-1)
                        elif self.settings['dataset'] == 'EpilepsiaSurf':
                            stft_data = stft_data[:, :, 1:]

                        proj = np.sum(stft_data, axis=(0, 1), keepdims=False)
                        self.global_proj += proj / 1000.0

                        #stft_data = np.multiply(stft_data,1.0/scale_)

                        stft_data = stft_data.reshape(-1, stft_data.shape[0],
                                                      stft_data.shape[1],
                                                      stft_data.shape[2])
                        #print (proj)

                        X_temp.append(stft_data)
                        # to differentiate between non overlapped and overlapped
                        # samples. Testing only uses non overlapped ones.
                        y_temp.append(2)
                        i += 1

                X_temp = np.concatenate(X_temp, axis=0)
                y_temp = np.array(y_temp)
                X.append(X_temp)
                y.append(y_temp)

            if ictal or interictal:
                #y = np.array(y)
                print('X', len(X), X[0].shape, 'y', len(y), y[0].shape)
                return X, y
            else:
                print('X', X.shape)
                return X
示例#59
0
def test_window_types(signal, framelength, window):
    """
    Test if callable and fixed value windows work

    """
    stft.spectrogram(signal, framelength=framelength, window=window)