def plotFFT(filename):
    """Plots single sided FFT"""
    fs_rate, signal = wavfile.read(filename)
    len_audio = len(signal.shape)
    print(signal.shape)
    print(signal[:][0])
    if len_audio == 2:
        signal = signal.sum(axis=1) / 2
    N = signal.shape[0]
    FFT = abs(scipy.fft(signal))
    FFT_side = FFT[range(N // 2)]
    freqs = scipy.fftpack.fftfreq(signal.size, 1.0 / fs_rate)
    fft_freqs = np.array(freqs)
    freqs_side = freqs[range(N // 2)]  # one side frequency range
    plt.plot(freqs_side, abs(FFT_side),
             "b")  # plotting the complete fft spectrum
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Single-sided Amplitude')
    plt.show()
Esempio n. 2
0
    def __init__(self, fs, signal):
        self.name = ''
        self.save = True
        self.signal = signal
        self.fs = fs # Sampling frequency
        self.shape = signal.shape
        self.Ns = 0 # Total num of samples

        # Dual-channel signals will be averaged to single channel.
        if (self.shape[0] == 2):
            self.signal = signal.sum(axis=1) / 2
            self.Ns = self.shape[1]
        else:
            self.Ns = self.shape[0]

        self.secs = self.Ns / fs # Length of track
        self.Ts = 1.0/fs # Sample interval

        self.t = np.arange(0, self.secs, self.Ts)

        self.ft = self.Ts * np.fft.fft(self.signal)
        self.ftfreq = np.fft.fftfreq(self.Ns, d=self.Ts)
        self.ft_magn = np.abs(self.ft)
def printPlotWav(filename):
    # Note: this code is borrowed
    # Note: FFTs are seem to be slow in python
    fs_rate, signal = wavfile.read(filename)
    print("Frequency sampling", fs_rate)
    len_audio = len(signal.shape)
    print("Channels", len_audio)
    if len_audio == 2:
        signal = signal.sum(axis=1) / 2
    N = signal.shape[0]
    print("Number of Samplings N", N)
    secs = N / float(fs_rate)
    print("secs", secs)
    Ts = 1.0 / fs_rate  # sampling interval in time
    print("Timestep between samples Ts", Ts)
    t = scipy.arange(0, secs,
                     Ts)  # time vector as scipy arange field / numpy.ndarray
    FFT = abs(scipy.fft(signal))
    FFT_side = FFT[range(N // 2)]  # one side FFT range
    freqs = scipy.fftpack.fftfreq(signal.size, t[1] - t[0])
    fft_freqs = np.array(freqs)
    freqs_side = freqs[range(N // 2)]  # one side frequency range
    fft_freqs_side = np.array(freqs_side)
    plt.subplot(311)
    p1 = plt.plot(t, signal, "g")  # plotting the signal
    plt.xlabel('Time')
    plt.ylabel('Amplitude')
    plt.subplot(312)
    p2 = plt.plot(freqs, FFT, "r")  # plotting the complete fft spectrum
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Count dbl-sided')
    plt.subplot(313)
    p3 = plt.plot(freqs_side, abs(FFT_side),
                  "b")  # plotting the positive fft spectrum
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Count single-sided')
    plt.show()
Esempio n. 4
0
def dc_offset(signal):
    """Correct DC offset"""
    log.debug('DC offset before: %.1f', np.sum(signal) / len(signal))
    signal = signal - signal.sum(dtype=np.int64) / len(signal)
    log.debug('DC offset after:  %.1f', np.sum(signal) / len(signal))
    return signal
Esempio n. 5
0
def gen_sec(intensity_array, eventid, sub_factor=32, plot_sec=False):

    fig = plt.figure(figsize = (35, 20))

    arr = subband(intensity_array, sub_factor)
    
    arr = bandpasscorr_sec(arr)
    
    summ, summask, masked_arr, component_class = nonoise(arr, 0)
    
    #Make a secondary spectrum
    hamm = np.hamming(len(arr[0]))
    arr_tap = arr * hamm
    ss = fftshift(fft2(arr_tap))
    ss = ss*np.conjugate(ss)
    ss_full = ss.real
    ss_crop = ss_full[256:306, 60:195]
    #ss = ss.real

    ax1 = fig.add_subplot(231)

    plt.imshow(arr, aspect = 'auto')
    plt.gca().invert_yaxis()
    plt.title(str(eventid) + " Dynamic Spectrum")
    plt.xlabel("Time (ms)")
    plt.ylabel("Frequency (MHz)")
    y = np.arange(0, len(arr.sum(1)), 64)
    x = np.arange(0, len(arr.sum(0)), 32)
    plt.yticks(y, np.arange(400, 800, 50))
    plt.xticks(x, np.arange(0, 256, 32))

    ax2 = fig.add_subplot(232)

    plt.plot(arr.sum(0))
    plt.title(str(eventid) + " Timeseries")
    plt.xlabel("Time (ms)")
    x = np.arange(0, len(arr.sum(0)), 32)
    plt.xticks(x, np.arange(0, 256, 32))

    ax3 = fig.add_subplot(233)

    plt.plot(arr.sum(1))
    plt.title(str(eventid) + " Spectrum")
    plt.xlabel("Frequency (MHz)")
    x = np.arange(0, len(arr.sum(1)), 64)
    plt.xticks(x, np.arange(400, 800, 50))

    ax4 = fig.add_subplot(234)

    #plt.plot(ss.sum(0))
    plt.imshow(ss_full, norm = LogNorm(), aspect = 'auto')
    plt.gca().invert_yaxis()
    plt.title(str(eventid)+ " " + "Secondary Spectrum")
    plt.ylabel("Time Delay " + r"($\mu$s)")
    plt.xlabel("Fringe Frequency " + "(Hz)")
    y = np.arange(0, len(ss.sum(1)), 64)
    x = np.arange(0, len(ss.sum(0)), 32)
    plt.yticks(y, np.arange(-2.5, 2.5, 0.625))
    plt.xticks(x, np.arange(-4, 4, 1))

    ax5 = fig.add_subplot(235)

    plt.imshow(ss_crop, norm = LogNorm(), aspect = 'auto')
    plt.gca().invert_yaxis()
    plt.title(str(eventid)+ " " + "Secondary Spectrum Cropped")
    plt.ylabel("Time Delay " + r"($\mu$s)")
    plt.xlabel("Fringe Frequency " + "(Hz)")
    y = np.arange(0, len(ss_crop.sum(1)), 6.2)
    x = np.arange(0, len(ss_crop.sum(0)), 11)
    plt.xticks(x, np.arange(-0.8, 0.8, 0.032))
    plt.yticks(y, np.arange(0, 0.128, 0.016))

    ax6 = fig.add_subplot(236)

    plt.imshow(ss_crop, aspect = 'auto')
    plt.gca().invert_yaxis()
    plt.title(str(eventid)+ " " + "Secondary Spectrum Cropped (No LogNorm)")
    plt.ylabel("Time Delay " + r"($\mu$s)")
    plt.xlabel("Fringe Frequency " + "(Hz)")
    y = np.arange(0, len(ss_crop.sum(1)), 6.2)
    x = np.arange(0, len(ss_crop.sum(0)), 11)
    plt.xticks(x, np.arange(-0.8, 0.8, 0.032))
    plt.yticks(y, np.arange(0, 0.128, 0.016))
    
    plt.tight_layout()
    fig.savefig(str(eventid) + " Secondary Spectrum")

    return
Esempio n. 6
0
def anchorUpdateSK(signal,
                   kernel,
                   signal_deconv=np.float32(0),
                   iterations=10,
                   measure=True,
                   clip=False,
                   verbose=True):

    # for code agnosticity between Numpy/Cupy
    xp = pyb.get_array_module(signal)
    xps = cupyx.scipy.get_array_module(signal)

    # for performance evaluation
    start_time = time.time()

    if iterations < 100:
        breakcheck = iterations
    else:
        breakcheck = 100

    # normalization
    signal /= signal.sum()
    epsilon = 1e-7

    # starting guess with a flat image
    if signal_deconv.any() == 0:
        # xp.random.seed(0)
        signal_deconv = xp.full(signal.shape,
                                0.5) + 0.01 * xp.random.rand(*signal.shape)
        # signal_deconv = signal.copy()
    else:
        signal_deconv = signal_deconv  #+ 0.1*prior.max()*xp.random.rand(*signal.shape)

    # normalization
    signal_deconv = signal_deconv / signal_deconv.sum()

    # to measure the distance between the guess convolved and the signal
    error = None
    if measure == True:
        error = xp.zeros(iterations)

    for i in range(iterations):
        # I use this property to make computation faster
        kernel_update = pyconv.correlate(signal_deconv,
                                         kernel,
                                         mode='same',
                                         method='fft')

        kernel_mirror = axisflip(kernel_update)

        relative_blur = pyconv.correlate(signal_deconv,
                                         kernel_update,
                                         mode='same',
                                         method='fft')

        # compute the measured distance metric if given
        if measure == True:
            # error[i] = xp.linalg.norm(signal/signal.sum()-relative_blur/relative_blur.sum())
            error[i] = snrIntensity_db(
                signal / signal.sum(),
                xp.abs(signal / signal.sum() -
                       relative_blur / relative_blur.sum()))
            if (error[i] < error[i - breakcheck]) and i > breakcheck:
                break

        if verbose == True and (i % 100) == 0 and measure == False:
            print('Iteration ' + str(i))
        elif verbose == True and (i % 100) == 0 and measure == True:
            print('Iteration ' + str(i) + ' - noise level: ' + str(error[i]))

        relative_blur = signal / relative_blur

        # # avoid errors due to division by zero or inf
        # relative_blur[xp.isinf(relative_blur)] = epsilon
        # relative_blur = xp.nan_to_num(relative_blur)

        # multiplicative update, for the full model
        # signal_deconv *= 0.5 * (pyconv.convolve(relative_blur, kernel_mirror, mode='same') + pyconv.correlate((relative_blur), kernel_mirror, mode='same'))
        # signal_deconv *= (my_convolution(relative_blur, kernel_mirror) + my_correlation(relative_blur,kernel_mirror))

        # multiplicative update, for the Anchor Update approximation
        signal_deconv *= pyconv.correlate(relative_blur,
                                          kernel_mirror,
                                          mode='same',
                                          method='fft')

        # multiplicative update, remaining term. This gives wrong reconstructions
        # signal_deconv *= pyconv.correlate((relative_blur), kernel_mirror, mode='same')

    if clip:
        signal_deconv[signal_deconv > +1] = +1
        signal_deconv[signal_deconv < -1] = -1

    print("\n\n Algorithm finished. Performance:")
    print("--- %s seconds ----" % (time.time() - start_time))
    print("--- %s sec/step ---" % ((time.time() - start_time) / iterations))
    return signal_deconv, error  #,kernel_update
Esempio n. 7
0
def anchorUpdateZ(signal,
                  kernel,
                  signal_deconv=np.float32(0),
                  kerneltype='B',
                  iterations=10,
                  measure=True,
                  clip=False,
                  verbose=True):
    """
    Reconstruction of signal_deconv from its auto-correlation signal, via a 
    RichardsonLucy-like multiplicative procedure. At the same time, the kernel 
    psf is deconvolved from the reconstruction so that the iteration converges
    corr(conv(signal_deconv, kernel), conv(signal_deconv, kernel),) -> signal.

    Parameters
    ----------
    signal : ndarray, either numpy or cupy. 
        The auto-correlation to be inverted
    kernel : ndarray, either numpy or cupy.
        Point spread function that blurred the signal. It must be 
        signal.shape == kernel.shape.
    signal_deconv : ndarray, either numpy or cupy or 0. It must be signal.shape == signal_deconv.shape.
        The de-autocorrelated signal deconvolved with kernel at ith iteration. The default is np.float32(0).
    kerneltype : string.
        Type of kernel update used for the computation choosing from blurring 
        directly the autocorrelation 'A', blurring the signal that is then 
        autocorrelated 'B' and the window applied in fourier domain 'C'. 
        The default is 'B'.
    iterations : int, optional
        Number of iteration to be done. The default is 10.
    measure : boolean, optional
        If true computes the euclidean distance between signal and the 
        auto-correlation of signal_deconv. The default is True.
    clip : boolean, optional
        Clip the results within the range -1 to 1. Useless for the moment. The default is False.
    verbose : boolean, optional
        Print current step value. The default is True.

    Returns
    -------
    signal_deconv : ndarray, either numpy or cupy.
        The de-autocorrelated signal deconvolved with kernel at ith iteration..
    error : vector.
        Euclidean distance between signal and the auto-correlation of signal_deconv.
        Last implementation returns the SNR instead of euclidean distance.

    """

    # for code agnosticity between Numpy/Cupy
    xp = pyb.get_array_module(signal)

    # for performance evaluation
    start_time = time.time()

    if iterations < 100:
        breakcheck = iterations
    else:
        breakcheck = 100

    # normalization
    signal /= signal.sum()
    kernel /= kernel.sum()
    epsilon = 1e-7

    # starting guess with a flat image
    if signal_deconv.any() == 0:
        # xp.random.seed(0)
        signal_deconv = xp.full(signal.shape,
                                0.5) + 0.01 * xp.random.rand(*signal.shape)
        # signal_deconv = signal.copy()
    else:
        signal_deconv = signal_deconv  #+ 0.1*prior.max()*xp.random.rand(*signal.shape)

    # normalization
    signal_deconv = signal_deconv / signal_deconv.sum()

    # to measure the distance between the guess convolved and the signal
    error = None
    if measure == True:
        error = xp.zeros(iterations)

    for i in range(iterations):
        # I use this property to make computation faster
        K = my_convolution(signal_deconv, my_correlation(kernel, kernel))

        relative_blur = my_correlation(K, signal_deconv)

        # compute the measured distance metric if given
        if measure == True:
            #error[i] = xp.linalg.norm(signal/signal.sum()-relative_blur/relative_blur.sum())
            error[i] = snrIntensity_db(
                signal / signal.sum(),
                xp.abs(signal / signal.sum() -
                       relative_blur / relative_blur.sum()))
            if (error[i] < error[i - breakcheck]) and i > breakcheck:
                break

        if verbose == True and (i % 100) == 0 and measure == False:
            print('Iteration ' + str(i))
        elif verbose == True and (i % 100) == 0 and measure == True:
            print('Iteration ' + str(i) + ' - noise level: ' + str(error[i]))

        relative_blur = signal / relative_blur

        # avoid errors due to division by zero or inf
        relative_blur[xp.isinf(relative_blur)] = epsilon
        relative_blur = xp.nan_to_num(relative_blur)

        # multiplicative update, for the full model
        # signal_deconv *= 0.5 * (my_convolution(relative_blur, kernel_mirror) + my_correlation(axisflip(relative_blur), kernel_mirror))
        # signal_deconv *= (my_convolution(kernel_mirror,relative_blur) + my_correlation(relative_blur, kernel_mirror))

        # multiplicative update, for the Anchor Update approximation
        signal_deconv *= my_correlation((relative_blur), (K))
        # signal_deconv *= (my_correlation(relative_blur, K) + my_convolution(relative_blur, K))

        # multiplicative update, remaining term. This gives wrong reconstructions
        # signal_deconv *= my_correlation(axisflip(relative_blur), kernel_mirror)

    if clip:
        signal_deconv[signal_deconv > +1] = +1
        signal_deconv[signal_deconv < -1] = -1

    print("\n\n Algorithm finished. Performance:")
    print("--- %s seconds ----" % (time.time() - start_time))
    print("--- %s sec/step ---" % ((time.time() - start_time) / iterations))
    return signal_deconv, error  #,kernel_update
Esempio n. 8
0
def maxAPosteriori(signal,
                   kernel,
                   iterations=10,
                   measure=True,
                   clip=True,
                   verbose=False):
    """
    Deconvolution using the Maximum a Posteriori algorithm. Implementation 
    identical to Richardson Lucy algorithm but with a different moltiplicative
    rule for the update.

    Parameters
    ----------
    signal : ndarray, either numpy or cupy. 
        The signal to be deblurred.
    kernel : ndarray, either numpy or cupy. 
        Point spread function that blurred the signal. It must be 
        signal.shape == kernel.shape.
    prior : ndarray, either numpy or cupy, optional
        the prior information to start the reconstruction. The default is np.float32(0).
    iterations : integer, optional
        Number of iteration to be done. The default is 10.
    measure : boolean, optional
        If true computes the euclidean distance between signal and the auto-correlation of signal_deconv. The default is True.
    clip : boolean, optional
        Clip the results within the range -1 to 1. The default is False.
    verbose : boolean, optional
        Print current step value. The default is True.

    Returns
    -------
    signal_deconv : ndarray, either numpy or cupy.
        The deconvolved signal with respect the given kernel at ith iteration.
    error : one dimensional ndarray.
        Euclidean distance between signal and the auto-correlation of signal_deconv.

    """
    xp = pyb.get_array_module(signal)
    start_time = time.time()

    epsilon = 1e-7

    # starting guess with a flat image
    if prior.any() == 0:
        signal_deconv = xp.full(signal.shape,
                                0.5) + 0.01 * xp.random.rand(*signal.shape)
    else:
        signal_deconv = prior  #+ 0.1*prior.max()*xp.random.rand(*signal.shape)

    kernel_mirror = axisflip(kernel)

    error = None
    if measure == True:
        error = xp.zeros(iterations)

    for i in range(iterations):
        if verbose == True and (i % 100) == 0:
            print('Iteration ' + str(i))

        relative_blur = my_convolution(signal_deconv, kernel)
        if measure == True:
            error[i] = xp.linalg.norm(signal / signal.sum() -
                                      relative_blur / relative_blur.sum())
        relative_blur = signal / relative_blur

        # avoid errors due to division by zero or inf
        relative_blur[xp.isinf(relative_blur)] = epsilon
        relative_blur = xp.nan_to_num(relative_blur)

        # multiplicative update given by the MAP
        signal_deconv *= xp.exp(
            my_convolution(relative_blur - 1, kernel_mirror))

    if clip:
        signal_deconv[signal_deconv > +1] = +1
        signal_deconv[signal_deconv < -1] = -1

    print("\n\n Algorithm finished. Performance:")
    print("--- %s seconds ----" % (time.time() - start_time))
    print("--- %s sec/step ---" % ((time.time() - start_time) / iterations))
    return signal_deconv, error
Esempio n. 9
0
import scipy.fftpack
import numpy as np
from matplotlib import pyplot as plt
import os
import glob

os.chdir('train/')
files = glob.glob('*.wav')
show = False  # True
err = 0
for filename in files:
    fs_rate, signal = wavfile.read(
        filename)  # fs_rate  cxzestotliwosc probkowania
    l_audio = len(signal.shape)  # liczba kanałow
    if l_audio == 2:
        signal = signal.sum(axis=1) / 2  # usrednianie jezeli stereo

    signal = signal[int(len(signal) / 20):-int(len(signal) / 20)]

    N = signal.shape[0]  # liczba probek
    secs = N / float(fs_rate)  # długosc nagrania
    Ts = 1.0 / fs_rate  # okres pomiedzy probkami
    t = scipy.arange(0, secs, Ts)  # czas odpowiadający próbkom

    signal = signal * np.kaiser(N, 12)  # zastosowanie kaisera

    FFT = abs(scipy.fft(signal))  # Transformata FFT
    freqs = scipy.fftpack.fftfreq(signal.size, Ts)  # wartosci czestotliwosci
    fft_freqs = np.array(freqs)

    min = 0