示例#1
0
def spectrogram(wavtemp, fs):
    # load analysis parameters
    params = load_params()
    new_fs = params['newFs']
    windowSize = params['windowSize']
    frameStep = params['frameStep']
    durationCut = params['durationCut']
    durationRCosDecay = params['durationRCosDecay']

    wavtemp = np.r_[wavtemp,np.zeros(16000)]

    if wavtemp.shape[0] > math.floor(durationCut * fs):
        wavtemp = wavtemp[:int(durationCut * fs)]
        wavtemp[wavtemp.shape[0] - int(fs * durationRCosDecay):] = wavtemp[
            wavtemp.shape[0] - int(
                fs * durationRCosDecay):] * utils.raised_cosine(
                    np.arange(int(fs * durationRCosDecay)), 0,
                    int(fs * durationRCosDecay))
    wavtemp = (wavtemp / 1.01) / (np.max(wavtemp) + np.finfo(float).eps)
    wavtemp = signal.resample(wavtemp, int(wavtemp.shape[0] / fs * new_fs))

    spectrogram__ = features.complexSpectrogram(wavtemp, windowSize, frameStep)
    repres = np.abs(spectrogram__[:int(spectrogram__.shape[0] / 2), :])

    return repres
示例#2
0
def mps(wavtemp, fs):
    # load analysis parameters
    params = load_params()
    new_fs = params['newFs']
    windowSize = params['windowSize']
    frameStep = params['frameStep']
    durationCut = params['durationCut']
    durationRCosDecay = params['durationRCosDecay']

    wavtemp = np.r_[wavtemp,np.zeros(16000)]

    if wavtemp.shape[0] > math.floor(durationCut * fs):
        wavtemp = wavtemp[:int(durationCut * fs)]
        wavtemp[wavtemp.shape[0] - int(fs * durationRCosDecay):] = wavtemp[
            wavtemp.shape[0] - int(
                fs * durationRCosDecay):] * utils.raised_cosine(
                    np.arange(int(fs * durationRCosDecay)), 0,
                    int(fs * durationRCosDecay))
    wavtemp = (wavtemp / 1.01) / (np.max(wavtemp) + np.finfo(float).eps)
    wavtemp = signal.resample(wavtemp, int(wavtemp.shape[0] / fs * new_fs))
    wavtemp = np.r_[np.zeros(1000), wavtemp, np.zeros(1000)]

    spectrogram__ = features.complexSpectrogram(wavtemp, windowSize, frameStep)
    repres = np.transpose(
        np.abs(spectrogram__[:int(spectrogram__.shape[0] / 2), :]))

    N = repres.shape[0]
    M = repres.shape[1]
    # spatial, temporal zeros padding
    N1 = 2**utils.nextpow2(repres.shape[0])
    N2 = 2 * N1
    M1 = 2**utils.nextpow2(repres.shape[1])
    M2 = 2 * M1

    Y = np.zeros((N2, M2),dtype=np.complex_)

    # % first fourier transform (w.r.t. frequency axis)
    for n in range(N):
        R1 = np.fft.fft(repres[n, :], M2)
        Y[n, :] = R1[:M2]

    # % second fourier transform (w.r.t. temporal axis)
    for m in range(M2):
        R1 = np.fft.fft(Y[:N, m], N2)
        Y[:, m] = R1

    repres = np.absolute(Y[:, :int(Y.shape[1] / 2)])
    # %scaleRateAngle = angle(Y) ;

    return repres
示例#3
0
def spectrogram(wavtemp,
                audio_fs=44100,
                window_size=1024,
                frame_step=128,
                duration=0.25,
                duration_cut_decay=0.05,
                resampling_fs=16000,
                offset=0):
    wavtemp = np.r_[wavtemp, np.zeros(resampling_fs)]
    # if wavtemp.shape[0] > math.floor(duration * audio_fs):
    #     wavtemp = wavtemp[:int(duration * audio_fs)]
    #     wavtemp[wavtemp.shape[0] - int(
    #         audio_fs * duration_cut_decay):] = wavtemp[wavtemp.shape[0] - int(
    #             audio_fs * duration_cut_decay):] * utils.raised_cosine(
    #                 np.arange(int(audio_fs * duration_cut_decay)), 0,
    #                 int(audio_fs * duration_cut_decay))
    if wavtemp.shape[0] > math.floor(duration * audio_fs):
        offset_n = int(offset * audio_fs)
        duration_n = int(duration * audio_fs)
        duration_decay_n = int(duration_cut_decay * audio_fs)
        wavtemp = wavtemp[offset_n:offset_n + duration_n]
        if offset_n == 0:
            wavtemp[wavtemp.shape[0] - duration_decay_n:] = wavtemp[
                wavtemp.shape[0] - duration_decay_n:] * utils.raised_cosine(
                    np.arange(duration_decay_n), 0, duration_decay_n)
        else:
            wavtemp[wavtemp.shape[0] - duration_decay_n:] = wavtemp[
                wavtemp.shape[0] - duration_decay_n:] * utils.raised_cosine(
                    np.arange(duration_decay_n), 0, duration_decay_n)
            wavtemp[:
                    duration_decay_n] = wavtemp[:duration_decay_n] * utils.raised_cosine(
                        np.arange(duration_decay_n), duration_decay_n,
                        duration_decay_n)
    wavtemp = (wavtemp / 1.01) / (np.max(wavtemp) + np.finfo(float).eps)
    wavtemp = signal.resample(wavtemp,
                              int(wavtemp.shape[0] / audio_fs * resampling_fs))

    spectrogram_ = features.complexSpectrogram(wavtemp, window_size,
                                               frame_step)
    spectrogram_ = np.abs(spectrogram_[:int(spectrogram_.shape[0] / 2), :])
    return spectrogram_
示例#4
0
def strf(wavtemp, fs):
    # load analysis parameters
    params = load_params()
    new_fs = params['newFs']
    windowSize = params['windowSize']
    frameStep = params['frameStep']
    durationCut = params['durationCut']
    durationRCosDecay = params['durationRCosDecay']

    wavtemp = np.r_[wavtemp,np.zeros(16000)]

    if wavtemp.shape[0] > math.floor(durationCut * fs):
        wavtemp = wavtemp[:int(durationCut * fs)]
        wavtemp[wavtemp.shape[0] - int(fs * durationRCosDecay):] = wavtemp[
            wavtemp.shape[0] - int(
                fs * durationRCosDecay):] * utils.raised_cosine(
                    np.arange(int(fs * durationRCosDecay)), 0,
                    int(fs * durationRCosDecay))
    wavtemp = (wavtemp / 1.01) / (np.max(wavtemp) + np.finfo(float).eps)
    wavtemp = signal.resample(wavtemp, int(wavtemp.shape[0] / fs * new_fs))
    wavtemp = np.r_[np.zeros(1000), wavtemp, np.zeros(1000)]

    spectrogram__ = features.complexSpectrogram(wavtemp, windowSize, frameStep)
    repres = np.transpose(
        np.abs(spectrogram__[:int(spectrogram__.shape[0] / 2), :]))
    # print(spectrogram__.shape)
    # print(repres.shape)
    N = repres.shape[0]
    M = repres.shape[1]
    # spatial, temporal zeros padding
    N1 = 2**utils.nextpow2(repres.shape[0])
    N2 = 2 * N1
    M1 = 2**utils.nextpow2(repres.shape[1])
    M2 = 2 * M1

    Y = np.zeros((N2, M2))

    # % first fourier transform (w.r.t. frequency axis)
    for n in range(N):
        R1 = np.abs(np.fft.fft(repres[n, :], M2))
        Y[n, :] = R1[:M2]

    # % second fourier transform (w.r.t. temporal axis)
    for m in range(M2):
        R1 = np.abs(np.fft.fft(Y[:N, m], N2))
        Y[:, m] = R1

    MPS_repres = np.abs(Y[:, :int(Y.shape[1] / 2)])

    # %% fourier strf
    maxRate = fs / frameStep / 2  #; % max rate values
    maxScale = windowSize / (fs * 1e-3) / 2  #; % max scale value
    ratesVector = np.linspace(-maxRate + 5, maxRate - 5, num=22)
    deltaRates = ratesVector[1] - ratesVector[0]
    scalesVector = np.linspace(0, maxScale - 5, num=11)
    deltaScales = scalesVector[2] - scalesVector[1]

    overlapRate = .75
    overlapScale = .75
    stdRate = deltaRates / 2 * (overlapRate + 1)
    stdScale = deltaScales / 2 * (overlapScale + 1)

    maxRatePoints = int(len(MPS_repres) / 2)
    maxScalePoints = MPS_repres.shape[1]
    stdRatePoints = maxRatePoints * stdRate / maxRate
    stdScalePoints = maxScalePoints * stdScale / maxScale

    # %STRF_repres = np.zeros((2*M, N, length(ratesVector), length(scalesVector)))
    STRF_repres = np.zeros((N, M, len(ratesVector), len(scalesVector)))
    # print('STRF_repres', STRF_repres.shape)
    for iRate in range(len(ratesVector)):
        rateCenter = ratesVector[iRate]
        # %rate center in point
        if rateCenter <= 0:
            rateCenterPoint = maxRatePoints * (
                2 - np.abs(rateCenter) / maxRate)
        else:
            rateCenterPoint = maxRatePoints * np.abs(rateCenter) / maxRate

        for iScale in range(len(scalesVector)):
            scaleCenter = scalesVector[iScale]
            # %scale center in point
            scaleCenterPoint = maxScalePoints * np.abs(scaleCenter) / maxScale
            filterPoint = gaussianWdw2d(rateCenterPoint, stdRatePoints,
                                          scaleCenterPoint, stdScalePoints,
                                          np.linspace(
                                              1,
                                              2 * maxRatePoints,
                                              num=2 * maxRatePoints),
                                          np.linspace(
                                              1,
                                              maxScalePoints,
                                              num=maxScalePoints))

            MPS_filtered = MPS_repres * filterPoint
            MPS_quadrantPoint = np.c_[MPS_filtered, np.fliplr(MPS_filtered)]
            stftRec = np.fft.ifft(np.transpose(np.fft.ifft(MPS_quadrantPoint)))
            ll = len(stftRec)
            stftRec = np.transpose(
                np.r_[stftRec[:M, :N], stftRec[ll - M:ll, :N]])
            # !! taking real values
            STRF_repres[:, :, iRate, iScale] = np.abs(stftRec[:, :int(stftRec.shape[1] / 2)])

    return STRF_repres