Beispiel #1
0
def f():
    window_type = display['window_type']
    window_size = int(display['size'])
    p = float(display['param'])

    window = np.zeros(window_size)

    if window_type == 'square':
        window = np.ones(window_size)
    elif window_type == 'exponential':
        window = np.exp(np.arange(window_size) * p)
    elif window_type == 'hanning':
        window = np.hanning(window_size)
    elif window_type == 'blackman':
        window = np.blackman(window_size)
    elif window_type == 'ricker':
        window = ricker(window_size, p)
    elif window_type == 'gaussian':
        window = gaussian(window_size, p)
    elif window_type == 'barthann':
        window = barthann(window_size)
    elif window_type == 'flattop':
        window = flattop(window_size)
    elif window_type == 'cosine':
        window = cosine(window_size)
    elif window_type == 'triangle':
        window = triang(window_size)

    return window
Beispiel #2
0
    def real_synthesis(y, N=1024):
        """
            Method to compute the resynthesis of the MDCT.
            A complex input matrix is asummed as input, derived from DCT typeIV.

            Arguments   :
                y       : (2D Array) Complex Representation

            Returns     :
                xrec    : (1D Array) Time domain reconstruction

            Usage       : xrec = TimeFrequencyDecomposition.complex_synthesis(y, N)

        """
        # Parameters and windowing function design
        win = cosine(2 * N, True)
        win *= np.sum(win)
        lfb = len(win)
        nTimeSlots = y.shape[0]
        SignalLength = nTimeSlots * N + 2 * N

        # Synthesis matrices
        Cos, _ = TimeFrequencyDecomposition.coreModulation(win, N)

        # Initialization
        zcos = np.zeros((1, SignalLength), dtype=np.float32)

        # Perform Complex Synthesis
        for m in range(0, nTimeSlots):
            # for m in xrange(0, nTimeSlots):
            zcos[0, m * N:m * N + lfb] += np.dot((y[m, :]).T, Cos)

        xrec = zcos
        return xrec.T
Beispiel #3
0
    def real_synthesis(y, N = 1024):
        """
            Method to compute the resynthesis of the MDCT.
            A complex input matrix is asummed as input, derived from DCT typeIV.

            Arguments   :
                y       : (2D Array) Complex Representation

            Returns     :
                xrec    : (1D Array) Time domain reconstruction

            Usage       : xrec = TimeFrequencyDecomposition.complex_synthesis(y, N)

        """
        # Parameters and windowing function design
        win = cosine(2*N, True)
        win *= np.sum(win)
        lfb = len(win)
        nTimeSlots = y.shape[0]
        SignalLength = nTimeSlots * N + 2 * N

        # Synthesis matrices
        Cos, _ = TimeFrequencyDecomposition.coreModulation(win, N)

        # Initialization
        zcos = np.zeros((1, SignalLength), dtype = np.float32)

        # Perform Complex Synthesis
        for m in xrange(0, nTimeSlots):
            zcos[0, m * N : m * N + lfb] += np.dot((y[m, :]).T, Cos)

        xrec = zcos
        return xrec.T
Beispiel #4
0
def spec_mapping(wave) :
    wave = wave[0]
    wave = wave[:-1,:]
    window = signal.cosine(WINDOW_SIZE)

    spec = signal.spectrogram(wave, window=window, nperseg=WINDOW_SIZE, noverlap=OVERLAP, mode='psd')
    spec = np.swapaxes(spec[2], 2, 1)
    # The convnet wants a tensor4 in (batch, channel, image x, image y)
    # our batch here is the sequence in time.
    # There is only one channel
    spec = spec[:,np.newaxis,:,:]

    return (spec,)
Beispiel #5
0
    def prepare_data(self, data, regenerate=False) :
        data = data[self.samplerate*60*2:len(data)-self.samplerate*60*2]
        window = signal.cosine(WINDOW_SIZE)

        f_data = signal.spectrogram(data, window=window, nperseg=WINDOW_SIZE, noverlap=OVERLAP, mode='complex')
        f_data = np.swapaxes(f_data[2], 0, 1)
        real = np.real(f_data)
        rmax = np.max(real) if np.absolute(np.min(real)) < np.max(real) else np.absolute(np.min(real))
        real /= rmax
        cplx = np.imag(f_data)
        cmax = np.max(cplx) if np.absolute(np.min(cplx)) < np.max(cplx) else np.absolute(np.min(cplx))
        cplx /= cmax

        return np.append(real, cplx, axis=1)
    def real_synthesis(y):
        """
            Method to compute the resynthesis of the MDCT.
            A real valued input matrix is asummed as input, derived from DCT typeIV.

            Arguments   :
                y       : (2D Array) Real Representation (time frames x frequency sub-bands (N))

            Returns     :
                xrec    : (1D Array) Time domain reconstruction

        """
        # Parameters and windowing function design
        N = y.shape[1]
        win = cosine(2*N, True)
        lfb = len(win)
        nTimeSlots = y.shape[0]
        SignalLength = nTimeSlots * N + 2 * N

        # Check global variables in order to avoid
        # computing over and over again the transformation matrices.
        glvars = globals()

        if 'Cos' in glvars and ((glvars['Cos'].T).shape[1] == N):
            print('... using pre-computed transformation matrix')
            global Cos
            # Initialization
            zcos = np.zeros((1, SignalLength), dtype=np.float32)

            # Perform Synthesis
            for m in xrange(0, nTimeSlots):
                zcos[0, m * N: m * N + lfb] += np.dot((y[m, :]).T, Cos)

        else:
            print('... computing transformation matrix')
            # Synthesis marix
            Cos = TimeFrequencyDecomposition.coreModulation(win, N)

            # Initialization
            zcos = np.zeros((1, SignalLength), dtype=np.float32)

            # Perform Synthesis
            for m in xrange(0, nTimeSlots):
                zcos[0, m * N: m * N + lfb] += np.dot((y[m, :]).T, Cos)

        return zcos.T
Beispiel #7
0
def core_modulation(freq_subbands, window_size):
    """
        Method to produce Analysis and Synthesis matrices.
        -https://github.com/Js-Mim/ASP/

        Arguments              :
            freq_subbands      :   (int) Number of subbands
            window_size        :   (int) Window size
        Returns                :
            Cos                :   (2D Array) Cosine Modulated Polyphase Matrix
    """
    w = cosine(window_size)

    # just added the following profiling to compare the speed of the two methods
    #from profilehooks import profile
    #@profile
    def orig_method(freq_subbands, window_size, w):
        # Initialize Storing Variables
        cos_an = np.zeros((freq_subbands, window_size), dtype=np.float32)
        # Generate Matrices
        for k in xrange(0, freq_subbands):
            for n in xrange(0, window_size):
                cos_an[k, n] = w[n] * np.cos(
                    np.pi / freq_subbands * (k + 0.5) *
                    (n + 0.5 + freq_subbands / 2)) * np.sqrt(
                        2. / freq_subbands)
        return cos_an

    #@profile
    def scott_method(freq_subbands, window_size, w):
        # Generate Matrices
        kvec = np.arange(0, freq_subbands) + 0.5
        nvec = np.arange(0, window_size) + 0.5 + freq_subbands / 2
        cos_an = w * np.cos(np.pi / freq_subbands * kvec[np.newaxis].T *
                            nvec) * np.sqrt(2. / freq_subbands)
        return cos_an

    method = 'scott'
    if ('scott' == method):
        cos_an = scott_method(freq_subbands, window_size, w)
    else:
        cos_an = orig_method(freq_subbands, window_size, w)

    return cos_an.astype(np.float32, copy=False)
Beispiel #8
0
def plot_smoothed(time, data, avg_period, ax=None, **plot_args):
    """Plot a time series smoothed with a cosine kernel

    Parameters:
        time        Array of equally spaced time points
        data        Array of data to plot at those times
        avg_period  Number of time points to average over
                    (width of the averaging kernel)
        ax          Axes to plot into (default: current/new Axes)
        plot_args   Any additional arguments to pass to pyplot.plot()
    """
    print("Averaging period: {:g} fs".format(avg_period * (time[1] - time[0])))
    #krnl = np.ones(avg_period) / avg_period
    krnl = signal.cosine(avg_period)
    krnl = krnl / np.sum(krnl)
    data_ma = np.convolve(data, krnl, mode='valid')
    if ax is None:
        ax = pyplot.gca()
    ax.plot(time[avg_period / 2:-(avg_period / 2 - 1)], data_ma, **plot_args)
    def real_analysis(x, N = 1024):
        """
            Method to compute the subband samples from time domain signal x.
            A real valued output matrix will be computed using DCT.

            Arguments   :
                x       : (1D Array) Input signal
                N       : (int)      Number of sub-bands

            Returns     :
                y       : (2D Array) Real valued output of the analysis

        """
        # Parameters and windowing function design
        win = cosine(2*N, True)
        lfb = len(win)
        nTimeSlots = len(x)/N - 2

        # Initialization
        ycos = np.zeros((len(x)/N, N), dtype = np.float32)
        ysin = np.zeros((len(x)/N, N), dtype = np.float32)

        # Check global variables in order to avoid
        # computing over and over again the transformation matrices.
        glvars = globals()

        if 'Cos' in glvars and ((glvars['Cos'].T).shape[1] == N):
            print('... using pre-computed transformation matrices')
            global Cos
            # Perform Analysis
            for m in xrange(0, nTimeSlots):
                ycos[m, :] = np.dot(x[m * N: m * N + lfb], Cos.T)
        else :
            print('... computing transformation matrices')
            # Analysis Matrix
            Cos = TimeFrequencyDecomposition.coreModulation(win, N)
            # Perform Analysis
            for m in xrange(0, nTimeSlots):
                ycos[m, :] = np.dot(x[m * N: m * N + lfb], Cos.T)

        return ycos
Beispiel #10
0
    def prepare_data(self, data, regenerate=False):
        data = data[self.samplerate * 60 * 2:len(data) -
                    self.samplerate * 60 * 2]
        window = signal.cosine(WINDOW_SIZE)

        f_data = signal.spectrogram(data,
                                    window=window,
                                    nperseg=WINDOW_SIZE,
                                    noverlap=OVERLAP,
                                    mode='complex')
        f_data = np.swapaxes(f_data[2], 0, 1)
        real = np.real(f_data)
        rmax = np.max(real) if np.absolute(
            np.min(real)) < np.max(real) else np.absolute(np.min(real))
        real /= rmax
        cplx = np.imag(f_data)
        cmax = np.max(cplx) if np.absolute(
            np.min(cplx)) < np.max(cplx) else np.absolute(np.min(cplx))
        cplx /= cmax

        return np.append(real, cplx, axis=1)
Beispiel #11
0
def lp_filter(data, sample_rate=24.0, time_constant=0.15):
    """
    Filter a series with `time_constant` (use 0.15 s for pressure), and for
    a signal of `sample_rate` in Hertz (24 Hz for 911+).
    NOTE: 911+ systems do not require filter for temperature nor salinity.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from ctd import DataFrame, lp_filter
    >>> raw = DataFrame.from_cnv('../test/data/CTD-spiked-unfiltered.cnv.bz2',
    ...                          compression='bz2')
    >>> prc = DataFrame.from_cnv('../test/data/CTD-spiked-filtered.cnv.bz2',
    ...                          compression='bz2')
    >>> kw = dict(sample_rate=24.0, time_constant=0.15)
    >>> original = prc.index.values
    >>> unfiltered = raw.index.values
    >>> filtered = lp_filter(unfiltered, **kw)
    >>> fig, ax = plt.subplots()
    >>> ax.plot(original, 'k', label='original')
    >>> ax.plot(unfiltered, 'r', label='unfiltered')
    >>> ax.plot(filtered, 'g', label='filtered')
    >>> ax.legend()
    >>> ax.axis([33564, 33648, 1034, 1035])
    >>> plt.show()

    NOTES
    -----
    http://wiki.scipy.org/Cookbook/FIRFilter
    """

    if False:  # FIXME:
        cosine = signal.cosine(Wn)

    if True:  # Butter is closer to what SBE is doing with their cosine filter.
        Wn = (1.0 / time_constant) / (sample_rate * 2.0)
        b, a = signal.butter(2, Wn, "low")
        data = signal.filtfilt(b, a, data)

    return data
Beispiel #12
0
def lp_filter(data, sample_rate=24.0, time_constant=0.15):
    """
    Filter a series with `time_constant` (use 0.15 s for pressure), and for
    a signal of `sample_rate` in Hertz (24 Hz for 911+).
    NOTE: 911+ systems do not require filter for temperature nor salinity.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from ctd import DataFrame, lp_filter
    >>> raw = DataFrame.from_cnv('../test/data/CTD-spiked-unfiltered.cnv.bz2',
    ...                          compression='bz2')
    >>> prc = DataFrame.from_cnv('../test/data/CTD-spiked-filtered.cnv.bz2',
    ...                          compression='bz2')
    >>> kw = dict(sample_rate=24.0, time_constant=0.15)
    >>> original = prc.index.values
    >>> unfiltered = raw.index.values
    >>> filtered = lp_filter(unfiltered, **kw)
    >>> fig, ax = plt.subplots()
    >>> ax.plot(original, 'k', label='original')
    >>> ax.plot(unfiltered, 'r', label='unfiltered')
    >>> ax.plot(filtered, 'g', label='filtered')
    >>> ax.legend()
    >>> ax.axis([33564, 33648, 1034, 1035])
    >>> plt.show()

    NOTES
    -----
    http://wiki.scipy.org/Cookbook/FIRFilter
    """

    if False:  # FIXME:
        cosine = signal.cosine(Wn)

    if True:  # Butter is closer to what SBE is doing with their cosine filter.
        Wn = (1. / time_constant) / (sample_rate * 2.)
        b, a = signal.butter(2, Wn, 'low')
        data = signal.filtfilt(b, a, data)

    return data
Beispiel #13
0
def core_modulation(freq_subbands, window_size):
    """
        Method to produce Analysis and Synthesis matrices.
        -https://github.com/Js-Mim/ASP/
        Arguments              :
            freq_subbands      :   (int) Number of subbands
            window_size        :   (int) Window size
        Returns                :
            Cos                :   (2D Array) Cosine Modulated Matrix
    """
    w = cosine(window_size)
    # Initialize Storing Variables
    cos_an = np.zeros((freq_subbands, window_size), dtype=np.float32)

    # Generate Matrices
    for k in xrange(0, freq_subbands):
        for n in xrange(0, window_size):
            cos_an[k, n] = w[n] * np.cos(
                np.pi / freq_subbands * (k + 0.5) *
                (n + 0.5 + freq_subbands / 2)) * np.sqrt(2. / freq_subbands)

    return cos_an
Beispiel #14
0
    def complex_analysis(x, N=1024):
        """
            Method to compute the subband samples from time domain signal x.
            A complex output matrix will be computed using DCT and DST.

            Arguments   :
                x       : (1D Array) Input signal
                N       : (int)      Number of sub-bands

            Returns     :
                y       : (2D Array) Complex output of QMF analysis matrix (Cosine)

            Usage       : y = TimeFrequencyDecomposition.complex_analysis(x, N)

        """
        # Parameters and windowing function design
        win = cosine(2 * N, True)
        win /= np.sum(win)
        lfb = len(win)
        nTimeSlots = len(x) / N - 2

        # Initialization
        ycos = np.zeros((len(x) / N, N), dtype=np.float32)
        ysin = np.zeros((len(x) / N, N), dtype=np.float32)

        # Analysis Matrices
        Cos, Sin = TimeFrequencyDecomposition.coreModulation(win, N)

        # Perform Complex Analysis
        for m in range(0, nTimeSlots):
            # for m in xrange(0, nTimeSlots):
            ycos[m, :] = np.dot(x[m * N:m * N + lfb], Cos.T)
            ysin[m, :] = np.dot(x[m * N:m * N + lfb], Sin.T)

        y = ycos + 1j * ysin

        return y
Beispiel #15
0
    def complex_analysis(x, N = 1024):
        """
            Method to compute the subband samples from time domain signal x.
            A complex output matrix will be computed using DCT and DST.

            Arguments   :
                x       : (1D Array) Input signal
                N       : (int)      Number of sub-bands

            Returns     :
                y       : (2D Array) Complex output of QMF analysis matrix (Cosine)

            Usage       : y = TimeFrequencyDecomposition.complex_analysis(x, N)

        """
        # Parameters and windowing function design
        win = cosine(2*N, True)
        win /= np.sum(win)
        lfb = len(win)
        nTimeSlots = len(x)/N - 2

        # Initialization
        ycos = np.zeros((len(x)/N, N), dtype = np.float32)
        ysin = np.zeros((len(x)/N, N), dtype = np.float32)

        # Analysis Matrices
        Cos, Sin = TimeFrequencyDecomposition.coreModulation(win, N)

        # Perform Complex Analysis
        for m in xrange(0, nTimeSlots):
            ycos[m, :] = np.dot(x[m * N : m * N + lfb], Cos.T)
            ysin[m, :] = np.dot(x[m * N : m * N + lfb], Sin.T)

        y = ycos + 1j *  ysin

        return y
Beispiel #16
0
def stft(sig_in, winsize_samples, nfft_samples, overlap_percentage, wintype):

    hopsize_fraction = (100 - overlap_percentage) / 100
    hopsize = int(hopsize_fraction * winsize_samples)

    if wintype == 'cosine':
        win_analysis = cosine(winsize_samples, sym=False)
    elif wintype == 'blackmanharris':
        win_analysis = blackmanharris(winsize_samples, sym=False)
    elif wintype == 'blackman':
        win_analysis = blackman(winsize_samples, sym=False)
    elif wintype == 'hamming':
        win_analysis = hamming(winsize_samples, sym=False)
    elif wintype == 'hanning':
        win_analysis = hanning(winsize_samples, sym=False)

    # to make sure we don't run out of samples at the end of the signal
    numFrames = math.ceil(sig_in.size / hopsize) - int(1 / hopsize_fraction)

    # store only half of the spectrum (the other half is the complex conjugate)
    specsize = int(nfft_samples / 2 + 1)

    stft_spectrum = np.zeros(shape=(specsize, numFrames))
    stft_spectrum = stft_spectrum + 0.j

    # Transform the signal frame-by-frame and store the spectrum
    for idx in range(0, numFrames):
        startid = int(idx * hopsize)
        endid = startid + winsize_samples
        currentFrame = sig_in[startid:endid]

        # if nfft_samples>win_analysis.size, the frame is zero-padded before FFT
        tmpspec = fft(win_analysis * currentFrame, nfft_samples)
        stft_spectrum[:, idx] = tmpspec[0:specsize]

    return stft_spectrum
Beispiel #17
0
# Plot the window and its frequency response:

from scipy import signal
from scipy.fftpack import fft, fftshift
import matplotlib.pyplot as plt

window = signal.cosine(51)
plt.plot(window)
plt.title("Cosine window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")

plt.figure()
A = fft(window, 2048) / (len(window) / 2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the cosine window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()
import numpy as np
import h5py
from scipy.io.wavfile import read
from scipy import signal

WINDOW_SIZE = 512
OVERLAP = 512/2

data = read("/data/lisatmp3/mastropo/XqaJ2Ol5cC4.wav")

samplerate = data[0]
data = data[1]

data = data[samplerate*60*1:len(data)-samplerate*60*1]
window = signal.cosine(WINDOW_SIZE)

f_data = signal.spectrogram(data, window=window, nperseg=WINDOW_SIZE, noverlap=OVERLAP, mode='complex')
f_data = np.swapaxes(f_data[2], 0, 1)
real = np.real(f_data)
rmax = np.max(real) if np.absolute(np.min(real)) < np.max(real) else np.absolute(np.min(real))
real /= rmax
cplx = np.imag(f_data)
cmax = np.max(cplx) if np.absolute(np.min(cplx)) < np.max(cplx) else np.absolute(np.min(cplx))
cplx /= cmax

data = np.append(real, cplx, axis=1)

f = h5py.File('/Tmp/mastropo/fouried_song.hdf5', mode='w')
frequency_sequence = f.create_dataset('frequency_sequence', (data.shape[0], data.shape[1]), dtype='float32')
frequency_sequence[...] = data
frequency_sequence.dims[0].label = 'time'
Beispiel #19
0
def istft(spectrum, winsize_samples, nfft_samples, overlap_percentage,
          wintype):

    hopsize_fraction = (100 - overlap_percentage) / 100
    hopsize = int(hopsize_fraction * winsize_samples)
    specsize = int(nfft_samples / 2 + 1)

    # get the synthesis window
    if wintype == 'cosine':
        win_synthesis = cosine(winsize_samples, sym=False)
    elif wintype == 'blackmanharris':
        win_synthesis = blackmanharris(winsize_samples, sym=False)
    elif wintype == 'blackman':
        win_synthesis = blackman(winsize_samples, sym=False)
    elif wintype == 'hamming':
        win_synthesis = hamming(winsize_samples, sym=False)
    elif wintype == 'hanning':
        win_synthesis = hanning(winsize_samples, sym=False)

    #make sure that the scaling is correct
    sqsum = np.sum(np.square(win_synthesis)) / winsize_samples
    invsqsum = 1 / sqsum
    tmp = hopsize_fraction
    correctionFactor = tmp * invsqsum
    win_synthesis = win_synthesis * correctionFactor

    # allocate memory for the signal
    numFrames = spectrum.shape[1]
    framesize = int((100 - overlap_percentage) * winsize_samples / 100)

    # allocate a 2D array for the time-domain signal (to be vectorized later)
    sig_hat = np.zeros(shape=(numFrames, framesize))

    # prepare buffers for the istft
    buffer_chunks = int(1 / hopsize_fraction)
    ifft_buffer = np.zeros(shape=(winsize_samples, buffer_chunks))

    for idx in range(0, numFrames):

        # get the current frame spectrum and append the complex conjugate
        tmpspec = np.zeros(shape=(nfft_samples)) + 0.j
        tmpspec[
            0:specsize] = spectrum[:,
                                   idx]  # select the column of the spectrogram
        tmpspec[specsize:nfft_samples] = np.conj(
            np.flip(spectrum[1:specsize - 1, idx], 0))

        # take the ifft of the spectrum
        tmpframe = np.real(ifft(tmpspec, nfft_samples))

        # apply the synthesis window to the time frame, and keep only the number of samples corresponding to the analysis window (if zero-padding was applied, this ensures that the irrelevant samples are discarded)
        frame_in_time = win_synthesis * tmpframe[0:winsize_samples]

        # put the last one out, take the new one in
        ifft_buffer[:, 1:buffer_chunks] = ifft_buffer[:, 0:buffer_chunks - 1]
        ifft_buffer[:, 0] = frame_in_time

        # implementation of the overlap-add procedure
        frame_out = np.zeros(shape=(hopsize))
        for bufidx in range(0, buffer_chunks):
            bufstart = bufidx * hopsize
            bufend = bufstart + hopsize
            frame_out = frame_out + ifft_buffer[bufstart:bufend, bufidx]

        # store the current output frame in the correct indices of the signal
        sig_hat[idx, :] = frame_out

        #--- end of for idx in range(0,numFrames)

    # vectorize 2D signal to obtain one time-domain signal vector
    sig_out = sig_hat.flatten()

    return sig_out
Beispiel #20
0
import numpy as np
import h5py
from scipy.io.wavfile import read
from scipy import signal

WINDOW_SIZE = 512
OVERLAP = 512 / 2

data = read("/data/lisatmp3/mastropo/XqaJ2Ol5cC4.wav")

samplerate = data[0]
data = data[1]

data = data[samplerate * 60 * 1:len(data) - samplerate * 60 * 1]
window = signal.cosine(WINDOW_SIZE)

f_data = signal.spectrogram(data,
                            window=window,
                            nperseg=WINDOW_SIZE,
                            noverlap=OVERLAP,
                            mode='complex')
f_data = np.swapaxes(f_data[2], 0, 1)
real = np.real(f_data)
rmax = np.max(real) if np.absolute(
    np.min(real)) < np.max(real) else np.absolute(np.min(real))
real /= rmax
cplx = np.imag(f_data)
cmax = np.max(cplx) if np.absolute(
    np.min(cplx)) < np.max(cplx) else np.absolute(np.min(cplx))
cplx /= cmax