Esempio n. 1
0
def nc_afsk1200Demod(sig, fs=48000.0, TBW=2.0):
    #  non-coherent demodulation of afsk1200
    # function returns the NRZ (without rectifying it)
    # 
    # sig  - signal
    # baud - The bitrate. Default 1200
    # fs   - sampling rate in Hz
    # TBW  - TBW product of the filters
    #
    # Returns:
    #     NRZ  
    # your code here
    taps = fs/1200-1
    bandpass = signal.firwin(taps, 1200, nyq=fs/2)
    spacepass = bandpass * np.exp(1j*2*np.pi*1200*np.r_[0.0:taps]/fs)
    markpass = bandpass * np.exp(1j*2*np.pi*3600*np.r_[0.0:taps]/fs)
    spaces = signal.fftconvolve(sig, spacepass, mode='same')
    marks = signal.fftconvolve(sig, markpass, mode='same')

    analog = np.abs(spaces)-np.abs(marks)
    lowpass = signal.firwin(taps, 2400*1.2, nyq=fs/2)
    filtered = signal.fftconvolve(analog, lowpass, mode='same')
    NRZ = filtered
    
    return NRZ
Esempio n. 2
0
def fm_afskDemod(sig, TBW=4, N=74, fs=48000.0):
    #TODO: add docstring
    #  non-coherent demodulation of afsk1200
    # function returns the NRZI (without rectifying it)
    baud = 1200.0
    bandwidth = 2*500.0 + baud

    #TODO fix this
    M = int(2/(bandwidth/fs))
    h = signal.firwin(74.0, bandwidth, nyq=fs/2)

    t = r_[0.0:len(h)]/fs
    fc = 1700.0
    h = h*np.exp(1j*2*np.pi*fc*t)
    output = signal.fftconvolve(h, sig)

    temp = output*np.conjugate(np.roll(output, 1))
    NRZ_fm = np.angle(temp)/3

    h2 = signal.firwin(74.0, 1200, nyq=fs/2)
    NRZ_fm = signal.fftconvolve(NRZ_fm, h2)

    NRZ_fm = (NRZ_fm*fs/(2.0*np.pi)-550.0)/500.0

    return NRZ_fm
 def test_scaling(self):
     """
     For one lowpass, bandpass, and highpass example filter, this test
     checks two things:
       - the mean squared error over the frequency domain of the unscaled
         filter is smaller than the scaled filter (true for rectangular
         window)
       - the response of the scaled filter is exactly unity at the center
         of the first passband
     """
     N = 11
     cases = [
         ([.5], True, (0, 1)),
         ([0.2, .6], False, (.4, 1)),
         ([.5], False, (1, 1)),
     ]
     for cutoff, pass_zero, expected_response in cases:
         h = firwin(N, cutoff, scale=False, pass_zero=pass_zero, window='ones')
         hs = firwin(N, cutoff, scale=True, pass_zero=pass_zero, window='ones')
         if len(cutoff) == 1:
             if pass_zero:
                 cutoff = [0] + cutoff
             else:
                 cutoff = cutoff + [1]
         assert_(self.mse(h, [cutoff]) < self.mse(hs, [cutoff]),
             'least squares violation')
         self.check_response(hs, [expected_response], 1e-12)
Esempio n. 4
0
    def demodulate2(self,samples):
        # DEMODULATION CODE

        # LIMITER goes here

        # low pass & down sampling
        h = signal.firwin(128,80000,nyq=1.2e5)
        lp_samples = signal.fftconvolve(samples, h)

    # polar discriminator

        A = lp_samples[1:lp_samples.size]
        B = lp_samples[0:lp_samples.size-1]

        dphase = ( A * np.conj(B) ) / np.pi

        dphase.resize(dphase.size+1)
        dphase[dphase.size-1] = dphase[dphase.size-2]

        h = signal.firwin(128,16000,nyq=1.2e5)
        rebuilt = signal.fftconvolve(dphase,h)

        output = rebuilt[::self.decim_r2]

        output = self.lowpass(output, self.audioFilterSize)

        return np.real(output)
Esempio n. 5
0
    def fir_filter(self, fir_ac=None, fir_dc=None, f_ac=None, f_dc=None,
                   a_ac=10, a_dc=10, alpha=None, filter_name=None, **kwargs):
        """Apply filters to generate the lock-in and dc components of phi"""

        if filter_name == 'bessel_matched':
            N_pts = kwargs.get('N_pts', int(self.ks / self.k0_dc * 6))
            dec = kwargs.get('dec', 32)
            n_pts_eval_fir = kwargs.get('n_pts_eval_fir', 2**16)
            window = kwargs.get('window', 'hann')

            fir_ac, fir_dc = _matched_filters(self.ks, self.x_m, N_pts, dec, window,
                                              n_pts_eval_fir)

            self.fir_ac = fir_ac
            self.fir_dc = fir_dc
        else:
            if fir_ac is None:
                if f_ac is None and alpha is None:
                    f_ac = self.fx * 0.5
                elif alpha is not None:
                    f_ac = self.v_tip/self.x_m * alpha
                self.fir_ac = signal.firwin(self.fs / (f_ac) * a_ac,
                                            f_ac, nyq=0.5 * self.fs,
                                            window='blackman')
            else:
                self.fir_ac = fir_ac

            if fir_dc is None:
                if f_dc is None and alpha is None:
                    f_dc = self.fx * 0.5
                elif alpha is not None:
                    f_dc = self.v_tip/self.x_m * alpha
                self.fir_dc = signal.firwin(self.fs/(f_dc) * a_dc,
                                            f_dc, nyq=0.5*self.fs,
                                            window='blackman')
            else:
                self.fir_dc = fir_dc

        indices = np.arange(self.phi.size)
        fir_ac_size = self.fir_ac.size
        fir_dc_size = self.fir_dc.size

        fir_max_size = max(fir_ac_size, fir_dc_size)

        self.m = indices[fir_max_size//2: -fir_max_size//2]
        self.tm = self.t[self.m]

        self._lock = np.exp(np.pi * 2j * self.fx * self.t)

        self.phi_lock = signal.fftconvolve(self.phi * self._lock * 2,
                                           self.fir_ac,
                                           mode='same')

        self.V_lock = self.phi_lock

        self.phi_lock_a = np.abs(self.phi_lock)
        self.phi_lock_phase = np.angle(self.phi_lock)

        self.phi_dc = signal.fftconvolve(self.phi, self.fir_dc, mode='same')
        self.V_dc = self.phi_dc
Esempio n. 6
0
def test_fir():
    print "Testing dsp-fir"

    #ref = ones(512)
    ref = (2.0 * random.rand(512)) - 1.0

    #test short mono fir
    writeaudio(ref)
    h = signal.firwin(21, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    writeaudio(expected, 'expected.wav')
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(expected, readaudio(), 1e-6)

    #test long mono fir
    writeaudio(ref)
    h = signal.firwin(312, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(expected, readaudio(), 1e-6)

    #test short stereo fir, mono coeffs
    writeaudio(transpose([ref,-ref]))
    h = signal.firwin(21, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(transpose([expected, -expected]), readaudio(), 1e-6)

    #test long stereo fir, mono coeffs
    writeaudio(transpose([ref,-ref]))
    h = signal.firwin(312, 0.4)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(transpose([expected, -expected]), readaudio(), 1e-6)

    #test asymmetric mono fir
    writeaudio(ref)
    impulse = concatenate(([1], zeros(499)))
    b, a = signal.butter(2, 500.0/24000, 'low')
    h = signal.lfilter(b, a, impulse)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(expected, readaudio(), 1e-6)

    #test asymmetric stereo fir
    writeaudio(transpose([ref,-ref]))
    impulse = concatenate(([1], zeros(499)))
    b, a = signal.butter(2, 500.0/24000, 'low')
    h = signal.lfilter(b, a, impulse)
    savetxt("test_coeffs.txt", h)
    expected = signal.lfilter(h, 1, ref)
    os.system("../file-qdsp -n 64 -i test_in.wav -o test_out.wav -p fir,h=test_coeffs.txt")
    compareaudio(transpose([expected, -expected]), readaudio(), 1e-6)

    os.remove('test_coeffs.txt')
Esempio n. 7
0
def designLinearBandpass(fa, fb, s_step, n=1001, show=False):
    f_sampling = 1/s_step    
    nyq = f_sampling / 2
    
    fa_ny = fa / nyq
    fb_ny = fb / nyq
    
    a = signal.firwin(n, cutoff = fa_ny, window = 'blackmanharris')
    #Highpass filter with spectral inversion
    b = - signal.firwin(n, cutoff = fb_ny, window = 'blackmanharris')
    # b[n/2] = b[n/2] + 1
    #Combine into a bandpass filter
    # d = - (a+b); d[n/2] = d[n/2] + 1


    b[nyq/2] = b[nyq/2] + 1
    #Combine into a bandpass filter
    d = - (a+b)
    d[nyq/2] = d[nyq/2] + 1


    #Frequency response
    if show:
        mfreqz(d)
        pylab.show()
    
    return d
Esempio n. 8
0
    def __init__(self, fs = 48000.0, Abuffer = 1024, Nchunks=43):
        
        #  Implementation of an afsk1200 TNC. 
        #
        #  The TNC processes a `Abuffer` long buffers, till `Nchunks` number of buffers are collected into a large one.
        #  This is because python is able to more efficiently process larger buffers than smaller ones.
        #  Then, the resulting large buffer is demodulated, sampled and packets extracted.
        #
        # Inputs:
        #    fs  - sampling rate
        #   TBW  -  TBW of the demodulator filters
        #   Abuffer - Input audio buffers from Pyaudio
        #   Nchunks - Number of audio buffers to collect before processing
        #   plla    - agressivness parameter of the PLL
        
        
        ## compute sizes based on inputs
        self.TBW = 2.0   # TBW for the demod filters
        self.N = (int(fs/1200*self.TBW)//2)*2+1   # length of the filters for demod
        self.fs = fs     # sampling rate   
        self.BW = self.TBW/(1.0*self.N/fs)      # BW of filter based on TBW
        self.Abuffer = Abuffer             # size of audio buffer
        self.Nchunks = Nchunks             # number of audio buffers to collect
        self.Nbuffer = Abuffer*Nchunks+self.N*3-3         # length of the large buffer for processing
        self.Ns = 1.0*fs/1200 # samples per symbol
        
        ## state variables for the modulator
        self.prev_ph = 0  # previous phase to maintain continuous phase when recalling the function
        
        ##  Generate Filters for the demodulator
        self.h_lp = signal.firwin(self.N,self.BW/fs*1.0,window='hanning')
        self.h_lpp = signal.firwin(self.N,self.BW*2*1.2/fs,window='hanning')
        self.h_space = self.h_lp*exp(1j*2*pi*(2200)*r_[-self.N/2:self.N/2]/fs)
        self.h_mark = self.h_lp*exp(1j*2*pi*(1200)*r_[-self.N/2:self.N/2]/fs)
        self.h_bp = signal.firwin(self.N,self.BW/fs*2.2,window='hanning')*exp(1j*2*pi*1700*r_[-self.N/2:self.N/2]/fs)


        ## PLL state variables  -- so conntinuity between buffers is preserved
        self.dpll = np.round(2.0**32 / self.Ns).astype(int32)    # PLL step
        self.pll =  0                # PLL counter
        self.ppll = -self.dpll       # PLL counter previous value -- to detect overflow
        self.plla = 0.74             # PLL agressivness (small more agressive)
        

        ## state variable to NRZI2NRZ
        self.NRZIprevBit = True  
        
        ## State variables for findPackets
        self.state='search'   # state variable:  'search' or 'pkt'
        self.pktcounter = 0   # counts the length of a packet
        self.packet = bitarray.bitarray([0,1,1,1,1,1,1,0])   # current packet being collected
        self.bitpointer = 0   # poiter to advance the search beyond what was already searched in the previous buffer

        ## State variables for processBuffer
        self.buff = zeros(self.Nbuffer)   # large overlapp-save buffer
        self.chunk_count = 0              # chunk counter
        self.oldbits = bitarray.bitarray([0,0,0,0,0,0,0])    # bits from end of prev buffer to be copied to beginning of new
        self.Npackets = 0                 # packet counter
Esempio n. 9
0
def fir_filt_prep(length, start_ind, stop_ind, numtaps=1000, window="blackmanharris"):
    """preps the FIR filter."""
    if start_ind==0:
        return firwin(numtaps, stop_ind, pass_zero=True,
                  nyq=length/2.0,
                  window=window)
    return firwin(numtaps, [start_ind, stop_ind], pass_zero=False,
                  nyq=length/2.0,
                  window=window)
Esempio n. 10
0
    def test_response(self):
        N = 51
        f = .5
        # increase length just to try even/odd
        h = firwin(N, f)  # low-pass from 0 to f
        self.check_response(h, [(.25,1), (.75,0)])

        h = firwin(N+1, f, window='nuttall')  # specific window
        self.check_response(h, [(.25,1), (.75,0)])

        h = firwin(N+2, f, pass_zero=False)  # stop from 0 to f --> high-pass
        self.check_response(h, [(.25,0), (.75,1)])

        f1, f2, f3, f4 = .2, .4, .6, .8
        h = firwin(N+3, [f1, f2], pass_zero=False)  # band-pass filter
        self.check_response(h, [(.1,0), (.3,1), (.5,0)])

        h = firwin(N+4, [f1, f2])  # band-stop filter
        self.check_response(h, [(.1,1), (.3,0), (.5,1)])

        h = firwin(N+5, [f1, f2, f3, f4], pass_zero=False, scale=False)
        self.check_response(h, [(.1,0), (.3,1), (.5,0), (.7,1), (.9,0)])

        h = firwin(N+6, [f1, f2, f3, f4])  # multiband filter
        self.check_response(h, [(.1,1), (.3,0), (.5,1), (.7,0), (.9,1)])

        h = firwin(N+7, 0.1, width=.03)  # low-pass
        self.check_response(h, [(.05,1), (.75,0)])

        h = firwin(N+8, 0.1, pass_zero=False)  # high-pass
        self.check_response(h, [(.05,0), (.75,1)])
Esempio n. 11
0
 def init(self):                             
        self.count0 = 0
        self.count1 = 0
        
        # Open the wav music file
        MusicFile = open(self.musicFileName, 'r')
        
        # Read the music samples into an array
        self.MusicFileArray = MusicFile.read()
        self.MusicFileArray = self.MusicFileArray[:(len(self.MusicFileArray) - 1)]
        self.MusicFileArray = self.MusicFileArray.split("\n")
        
        # Filter0
        coeffFilter0 = signal.firwin(self.FilterDict["Filter0"]['N'], [self.FilterDict["Filter0"]["fc0"], self.FilterDict["Filter0"]["fc1"]], nyq = (0.5 * self.FilterDict["Filter0"]["fs"]), window = 'hamming')
        ## Calculate coefficients in C
        print "C implementation"
        set_trace()
        ###############
        for i in range(len(coeffFilter0)):
               coeffFilter0[i] = example.hamming_win(self.FilterDict["Filter0"]['N'], i, self.FilterDict["Filter0"]["fc1"], self.FilterDict["Filter0"]["fs"]) 
        
        
        set_trace()
        ##################
        
        
        coeffFilter0Fp = []      
        for i in coeffFilter0:
               coeffFilter0Fp.append(int(fp_sign_to_bin(i, self.fpNotation), 2))
       
        self.FilterDict["Filter0"]["Coefficients"] = coeffFilter0Fp
        
        # Filter1
        coeffFilter1 = signal.firwin(self.FilterDict["Filter1"]['N'], [self.FilterDict["Filter1"]["fc0"], self.FilterDict["Filter1"]["fc1"]], nyq = (0.5 * self.FilterDict["Filter1"]["fs"]), window = 'hamming')
        coeffFilter1Fp = []      
        for i in coeffFilter1:
               coeffFilter1Fp.append(int(fp_sign_to_bin(i, self.fpNotation), 2))
       
        self.FilterDict["Filter1"]["Coefficients"] = coeffFilter1Fp              
        
        # Filter2
        coeffFilter2 = signal.firwin(self.FilterDict["Filter2"]['N'], [self.FilterDict["Filter2"]["fc0"], self.FilterDict["Filter2"]["fc1"]], nyq = (0.5 * self.FilterDict["Filter2"]["fs"]), window = 'hamming')
        coeffFilter2Fp = []      
        for i in coeffFilter2:
               coeffFilter2Fp.append(int(fp_sign_to_bin(i, self.fpNotation), 2))
       
        self.FilterDict["Filter2"]["Coefficients"] = coeffFilter2Fp              
        
        # Filter3
        coeffFilter3 = signal.firwin(self.FilterDict["Filter3"]['N'], [self.FilterDict["Filter3"]["fc0"], self.FilterDict["Filter3"]["fc1"]], nyq = (0.5 * self.FilterDict["Filter3"]["fs"]), window = 'hamming')
        coeffFilter3Fp = []      
        for i in coeffFilter3:
               coeffFilter3Fp.append(int(fp_sign_to_bin(i, self.fpNotation), 2))
       
        self.FilterDict["Filter3"]["Coefficients"] = coeffFilter3Fp
Esempio n. 12
0
def get_h_parameters(NFIR, fcut):
    """NFIR : length of FIR filter
    fcut: fraction of Nyquist for filter"""
    h = si.firwin(NFIR+1,fcut) * np.exp(2*1j*np.pi*np.arange(NFIR+1)*0.125)
    n = np.arange(2,NFIR+2)
    g = h[(1-n)%NFIR]*(-1)**(1-n)
    NFIR = np.fix((3./2.*NFIR))
    h1 = si.firwin(NFIR+1,2./3*fcut)*np.exp(2j*np.pi*np.arange(NFIR+1)*0.25/3.)
    h2 = h1*np.exp(2j*np.pi*np.arange(NFIR+1)/6.)
    h3 = h1*np.exp(2j*np.pi*np.arange(NFIR+1)/3.)  
    return (h, g, h1, h2, h3)
Esempio n. 13
0
    def process(self,samples):

        #samples = sdr.read_samples(2.56e6)

        h = signal.firwin(256,80000,nyq=1.2e5)
        output = signal.fftconvolve(samples,h)
        output[:h.size/2] += self.prevConv1[h.size/2:]   #add the latter half of tail end of the previous convolution
        outputa = np.append(self.prevConv1[:h.size/2], output) # also delayed by half size of h so append the first half
        self.prevConv1 = output[output.size-h.size:]   # set the tail for next iteration
        lp_samples = outputa[:output.size-h.size]  # chop off the tail and decimate

        #lp_samples = output[::5]

        dmod = np.zeros(lp_samples.size)

        A = lp_samples[1:]
        B = lp_samples[:lp_samples.size-1]

        dmod[1:] = np.real(np.angle(A * np.conj(B))) / (np.pi)
        dmod[0] = np.real(np.angle(lp_samples[0] * np.conj(self.prevB))) / (np.pi)
        self.prevB = lp_samples[lp_samples.size-1]



        h = signal.firwin(256,1.6e4,nyq=1.2e5)
        output = signal.fftconvolve(dmod,h)
        output[:h.size/2] += self.prevConv2[h.size/2:]   #add the latter half of tail end of the previous convolution
        outputa = np.append(self.prevConv2[:h.size/2], output) # also delayed by half size of h so append the first half
        self.prevConv2 = output[output.size-h.size:]   # set the tail for next iteration
        audible = outputa[:output.size-h.size:5]  # chop off the tail and decimate
    

        #h = signal.firwin(128,1.6e4,nyq=24000)
        #output = signal.fftconvolve(audible,h)
        #output[:h.size/2] += prevConv3[h.size/2:]   #add the latter half of tail end of the previous convolution
        #outputa = np.append(prevConv3[:h.size/2], output) # also delayed by half size of h so append the first half
        #prevConvo3 = output[output.size-h.size:]   # set the tail for next iteration
        #audible = outputa[:output.size-h.size:5]  # chop off the tail and decimate
    
        #print audible.size
        #spec = gen_spec(audible,256)
        #show_image(spec)
   
        self.spec = np.roll(self.spec,26,axis=1)

        self.spec[:,:26] = gen_spec(np.real(audible),512)   ##np.abs(np.fft.fft(audible)[:audible.size/2:-4])
        spec = cv2.GaussianBlur(self.spec,(5,5),1,.75)
        spectsc = cv2.convertScaleAbs(self.spec,alpha=255/np.max(spec))
        spect = cv2.applyColorMap(spectsc,cv2.COLORMAP_JET)
        
        cv2.imshow('Spectrum',spect)
        cv2.waitKey(1)

        return np.real(.5*audible)
Esempio n. 14
0
def createFilters():
    fs = 500 
    f_nyquist = fs/2 # Calling nyquist fs/2 because this corresponds to pi radians
    numtaps = 71 # must be less than sig_length / 3

    h_lp_ecg = signal.firwin(numtaps, 150./f_nyquist)
    h_lp_icg = signal.firwin(numtaps, 1./f_nyquist)
    h_lp_ppg = signal.firwin(numtaps, 30./f_nyquist) # Can be used for ICG - cardiac
    h_60 = signal.firwin(numtaps, [55./f_nyquist, 65./f_nyquist])
    h_hp = signal.firwin(numtaps, 5./f_nyquist, pass_zero=False)

    return(h_lp_ecg, h_lp_icg, h_lp_ppg, h_60, h_hp)
Esempio n. 15
0
 def bandreject(self, S, low=700, high=800):
     # http://www.scipy.org/Cookbook/FIRFilter
     # http://mpastell.com/2010/01/18/fir-with-scipy/
     nyq_rate = self.spl_rate / 2.0
     width = 50.0 / nyq_rate
     ripple_db = 60.0
     N, beta = signal.kaiserord(ripple_db, width)
     tapsL = signal.firwin(N, low / nyq_rate, window=("kaiser", beta))
     tapsH = signal.firwin(N, high / nyq_rate, window=("kaiser", beta))
     tapsB = -(tapsL + tapsH)
     tapsB[N / 2] = tapsB[N / 2] + 1
     return signal.lfilter(tapsB, 1.0, S)
Esempio n. 16
0
def nc_mafsk1200Demod(sig, fs=48000.0, baud=1200, TBW=2.0, fc = 2700, fd = 1000):
    #  non-coherent demodulation of afsk1200
    # function returns the NRZ (without rectifying it)
    # 
    # sig  - signal
    # baud - The bitrate. Default 1200
    # fs   - sampling rate in Hz
    # TBW  - TBW product of the filters
    #
    # Returns:
    #     NRZ 
    
    N = (int(fs/baud*TBW)//2)*2+1
    f11 = fc - 2*fd
    f10 = fc - fd
    f01 = fc + fd
    f00 = fc + 2*fd
    
    # your code here
    taps = TBW*fs/1200-1
    taps = N
    filt = signal.firwin(taps, baud/2, window='hanning', nyq=fs/2)
    #plt.plot(np.fft.fft(filt))
    #plt.plot(filt)
    #f1 = 1200
    #f2 = 2200
    t2 = (r_[0:fs]/fs)[:taps]
    filt11 = filt* np.exp(t2*1j*f11*-2*np.pi)
    filt10 = filt* np.exp(t2*1j*f10*-2*np.pi)
    sig11 = signal.fftconvolve(sig, filt11, mode="same")
    sig10 = signal.fftconvolve(sig, filt10, mode="same")
    filt01 = filt* np.exp(t2*1j*f01*-2*np.pi)
    filt00 = filt* np.exp(t2*1j*f00*-2*np.pi)
    sig01 = signal.fftconvolve(sig, filt01, mode="same")
    sig00 = signal.fftconvolve(sig, filt00, mode="same")
    midsig = 0#(max(sig00)+min(sig00)+max(sig11)+min(sig11))/4
    
    sig11r = sig11 - midsig
    sig10r = sig10 - midsig
    sig01r = sig01 - midsig
    sig00r = sig00 - midsig
    
    return sig11r, sig10r, sig01r, sig00r
    
    diff = np.abs(sig12k)-np.abs(sig22k)
    return diff
    opt = signal.firwin(taps, baud*1.2, window='hanning', nyq=fs/2)
    ana = signal.fftconvolve(diff, opt, mode="same")
    #sign = np.sign(ana)

    NRZ = ana
    return NRZ
Esempio n. 17
0
def initialize_online_filter(fsample, highpass, lowpass, order, x, axis=-1):
    # boxcar, triang, blackman, hamming, hann, bartlett, flattop, parzen, bohman, blackmanharris, nuttall, barthann
    filtwin = 'nuttall'
    nyquist = fsample / 2.
    ndim = len(x.shape)
    axis = axis % ndim

    if highpass != None:
        highpass = highpass/nyquist
        if highpass < 0.01:
            highpass = None
        elif highpass > 0.99:
            highpass = None

    if lowpass != None:
        lowpass = lowpass/nyquist
        if lowpass < 0.01:
            lowpass = None
        elif lowpass > 0.99:
            lowpass = None

    if not(highpass is None) and not(lowpass is None) and highpass>=lowpass:
        # totally blocking all signal
        print 'using NULL filter', [highpass, lowpass]
        b = np.zeros(window)
        a = np.ones(1)
    elif not(lowpass is None) and (highpass is None):
        print 'using lowpass filter', [highpass, lowpass]
        b = firwin(order, cutoff = lowpass, window = filtwin, pass_zero = True)
        a = np.ones(1)
    elif not(highpass is None) and (lowpass is None):
        print 'using highpass filter', [highpass, lowpass]
        b = firwin(order, cutoff = highpass, window = filtwin, pass_zero = False)
        a = np.ones(1)
    elif not(highpass is None) and not(lowpass is None):
        print 'using bandpass filter', [highpass, lowpass]
        b = firwin(order, cutoff = [highpass, lowpass], window = filtwin, pass_zero = False)
        a = np.ones(1)
    else:
        # no filtering at all
        print 'using IDENTITY filter', [highpass, lowpass]
        b = np.ones(1)
        a = np.ones(1)

    # initialize the state for the filtering based on the previous data
    if ndim == 1:
        zi = zi = lfiltic(b, a, x, x)
    elif ndim == 2:
        f = lambda x : lfiltic(b, a, x, x)
        zi = np.apply_along_axis(f, axis, x)

    return b, a, zi
Esempio n. 18
0
def testfir(st,cb,ct,n):
    from scipy import signal
    import numpy as np
    
    sr = st[0].stats.sampling_rate/2.
    xx = np.empty([st.count(),n],)
    a = signal.firwin(n, cutoff = cb/sr, window = 'hamming')
    b = - signal.firwin(n, cutoff = ct/sr, window = 'hamming'); b[n/2] = b[n/2] + 1
    d = - (a+b); d[n/2] = d[n/2] + 1
    fft1 = np.abs(np.fft.fft(d))
    for i in range(st.count()):
        fft = np.fft.fft(st[i][:n])*fft1
        xx[i] = np.fft.ifft(fft)
    return xx
Esempio n. 19
0
def bandpass_filter(signal, signal_rate, lowcut, highcut, window='hann'):
    """ Apply bandpass filter. Everything below 'lowcut' and above 'highcut'
        frequency level will be greatly reduced. """
    ntaps = 199
    nyq = 0.5 * signal_rate
    lowpass = firwin(ntaps, lowcut, nyq=nyq, pass_zero=False,
                     window=window, scale=False)
    highpass = - firwin(ntaps, highcut, nyq=nyq, pass_zero=False,
                        window=window, scale=False)
    highpass[ntaps/2] = highpass[ntaps/2] + 1
    bandpass = -(lowpass + highpass)
    bandpass[ntaps/2] = bandpass[ntaps/2] + 1
    filteredSignal = lfilter(bandpass, 1.0, signal)
    return filteredSignal
Esempio n. 20
0
    def __init__(self, fs = 48000, baud = 2400, fc = 1800):
        self.fs = fs
        self.fc = fc
        self.baud = baud
        self.f_mark = self.fc - baud / 4
        self.f_space = self.fc + baud / 4
        self.delta_f = (self.f_space - self.f_mark) / 2
        self.spb = self.fs / baud

        taps = 2 * int(fs * 2 / baud) + 1
        h = signal.firwin(taps, self.baud / 2., nyq = fs / 2., window = 'hanning')
        self.bp_mark = np.exp(2j * np.pi * self.f_mark * np.r_[0:taps] / float(fs)) * h
        self.bp_space = np.exp(2j * np.pi * self.f_space * np.r_[0:taps] / float(fs)) * h
        self.h_nrz = signal.firwin(taps, self.baud * 1.2, nyq = fs / 2., window = 'hanning')
Esempio n. 21
0
def bandpass(data, sampling, fmin, fmax, ripple_db=50, width=2.0,\
             return_filter=False, verbose=False):

  """
    This function will bandpass filter data in the given [fmin,fmax] band
    using a kaiser window.

    Arguments:

      data : numpy.ndarray
        array of data points
      sampling : int
        number of data points per second
      fmin : float
        frequency of lowpass
      fmax : float
        frequency of highpass

    Keyword arguments:

      ripple_db : int
        Attenuation in the stop band, in dB
      width : float
        Desired width of the transition from pass to stop, in Hz
      return_filter: boolean
        Return filter
      verbose : boolean
  """

  # construct filter
  order, beta = signal.kaiserord(ripple_db, width*2/sampling)

  lowpass = signal.firwin(order, fmin*2/sampling, window=('kaiser', beta))
  highpass = - signal.firwin(order, fmax*2/sampling, window=('kaiser', beta))
  highpass[order//2] = highpass[order//2] + 1

  bandpass = -(lowpass + highpass); bandpass[order//2] = bandpass[order//2] + 1

  # filter data forward then backward
  data = signal.lfilter(bandpass,1.0,data)
  data = data[::-1]
  data = signal.lfilter(bandpass,1.0,data)
  data = data[::-1]

  if verbose: sys.stdout.write("Bandpass filter applied to data.\n")

  if return_filter:
    return data, bandpass
  else:
    return data
Esempio n. 22
0
    def fir(self):
        """
        Filter the time-series using an FIR digital filter. Filtering is done
        back and forth (using scipy.signal.filtfilt) to achieve zero phase
        delay
        """
        # Passband and stop-band are expressed as fraction of the Nyquist
        # frequency:
        if self.ub is not None:
            ub_frac = self.ub / (self.sampling_rate / 2.0)
        else:
            ub_frac = 1.0

        lb_frac = self.lb / (self.sampling_rate / 2.0)

        if lb_frac < 0 or ub_frac > 1:
            e_s = "The lower-bound or upper bound used to filter"
            e_s += " are beyond the range 0-Nyquist. You asked for"
            e_s += " a filter between"
            e_s += "%s and %s percent of" % (lb_frac * 100, ub_frac * 100)
            e_s += "the Nyquist frequency"
            raise ValueError(e_s)

        n_taps = self._filt_order + 1

        # This means the filter order you chose was too large (needs to be
        # shorter than a 1/3 of your time-series )
        if n_taps > self.data.shape[-1] * 3:
            e_s = "The filter order chosen is too large for this time-series"
            raise ValueError(e_s)

        # a is always 1:
        a = [1]

        sig = ts.TimeSeries(data=self.data, sampling_rate=self.sampling_rate)

        # Lowpass:
        if ub_frac < 1:
            b = signal.firwin(n_taps, ub_frac, window=self._win)
            sig = self.filtfilt(b, a, sig)

        # High-pass
        if lb_frac > 0:
            # Includes a spectral inversion:
            b = -1 * signal.firwin(n_taps, lb_frac, window=self._win)
            b[n_taps / 2] = b[n_taps / 2] + 1
            sig = self.filtfilt(b, a, sig)

        return sig
Esempio n. 23
0
def nc_afsk_demod(sig, f_low, f_high, TBW=2.0, N=74,  fs = 44100.0):
    #  non-coherent demodulation of afsk1200
    # function returns the NRZI (without rectifying it)

    BW =  float(TBW) / N
    
    h_mark = signal.firwin(numtaps=N, cutoff=BW) * np.exp(1j * 2 * pi * f_low/fs * np.arange(N))
    h_space = signal.firwin(numtaps=N, cutoff=BW) * np.exp(1j * 2 * pi * f_high/fs * np.arange(N))

    v_space = signal.fftconvolve(sig, h_space)
    v_mark  = signal.fftconvolve(sig, h_mark)

    NRZa = abs(v_space) - abs(v_mark)

    return NRZa[N/2:-N/2]
Esempio n. 24
0
def demodulateQAM16(signal, fc, fs, symbol_length, predelay):
    symbol_size = fs*symbol_length
    num_symbols = signal.size // symbol_size
    n_taps = 640*2
    
    inphase = np.zeros_like(signal)
    quadphase = np.zeros_like(signal)

    h = sp.firwin(n_taps, fs/320, nyq=fs/2)         # perhaps try using a notch filter instead of a firwin 
    h *= np.cos(2*np.pi*fc/fs*np.r_[:h.size])       # since the bandwidth of a QAM channel is so small.
    #signal = sp.lfilter(h, 1.0, signal)
    signal = sp.fftconvolve(signal, h, mode='same')

    #spectrum(signal)
    #spectrogram(signal)
    #myplot(signal)

    # calculate phase adjustment:
    # using -2 instead of -1 beacuse something weird happens when adding to the np.r_[] below
    delay = 0.5*(n_taps-2) 
  
    # demodulation to baseband
    inphase = signal * np.cos(2*np.pi*fc/fs*(np.r_[:signal.size] + delay + predelay))
    quadphase = - signal * np.sin(2*np.pi*fc/fs*(np.r_[:signal.size] + delay + predelay))
        
    #lowpass after demodulating
    h = sp.firwin(n_taps/8, fs/160, nyq=fs/2)
    inphase = sp.lfilter(h, 1.0, inphase)
    quadphase = sp.lfilter(h, 1.0, quadphase)
    #inphase = sp.fftconvolve(inphase, h, mode = 'same')
    #quadphase = sp.fftconvolve(quadphase, h, mode='same')
    
    #myplot(inphase)
    #spectrogram(inphase)

  #  inphase = sp.medfilt(inphase, int(symbol_size/4)-1)
  #  quadphase = sp.medfilt(quadphase, int(symbol_size/4)-1)

    #myplot(inphase)
    #spectrogram(inphase)

    # calc delay:
    delay = 0.5 * (n_taps/8 -1)
    
    i_coef = inphase[symbol_size/2+delay::symbol_size]
    q_coef = quadphase[symbol_size/2+delay::symbol_size]

    return matchCode16(i_coef, q_coef)
Esempio n. 25
0
 def __init__(self,hfoObj):
     #signal = sig.detrend(hfoObj.waveform[hfoObj.start_idx:hfoObj.end_idx,0]) # detrending
     fs = hfoObj.sample_rate        
    
     signal = sig.detrend(hfoObj.waveform[3*fs/4:5*fs/4,0])
     PhaseFreqVector= np.arange(1,31,1)
     AmpFreqVector= np.arange(30,990,5)
     PhaseFreq_BandWidth=1
     AmpFreq_BandWidth=10
     Comodulogram=np.zeros((PhaseFreqVector.shape[0],AmpFreqVector.shape[0]))
     nbin=18
     position=np.zeros(nbin)
     winsize = 2*np.pi/nbin
     for j in range(nbin):
         position[j] = -np.pi+j*winsize;
     PHASES = np.zeros((PhaseFreqVector.shape[0],signal.shape[0]))
     for idx,Pf1 in enumerate(PhaseFreqVector):
         print Pf1,
         Pf2 = Pf1 + PhaseFreq_BandWidth
         if signal.shape[0] > 18*np.fix(fs/Pf1):
             b = sig.firwin(3*np.fix(fs/Pf1),[Pf1,Pf2],pass_zero=False,window=('kaiser',0.5),nyq=fs/2)
         else:
             b = sig.firwin(signal.shape[0]/6,[Pf1,Pf2],pass_zero=False,window=('kaiser',0.5),nyq=fs/2)
         PhaseFreq = sig.filtfilt(b,np.array([1]),signal)
         Phase=np.angle(sig.hilbert(PhaseFreq))
         PHASES[idx,:]=Phase;
     print    
     for idx1,Af1 in enumerate(AmpFreqVector):
         print Af1,
         Af2 = Af1 + AmpFreq_BandWidth
         if signal.shape[0] > 18*np.fix(fs/Af1):
             b = sig.firwin(3*np.fix(fs/Af1),[Af1,Af2],pass_zero=False,window=('kaiser',0.5),nyq=fs/2)
         else:
             b = sig.firwin(np.fix(signal.shape[0]/6),[Af1,Af2],pass_zero=False,window=('kaiser',0.5),nyq=fs/2)
         AmpFreq = sig.filtfilt(b,np.array([1]),signal)
         Amp=np.abs(sig.hilbert(AmpFreq))
         for idx2,Pf1 in enumerate(PhaseFreqVector):
             Phase = PHASES[idx2]
             MeanAmp = np.zeros(nbin)
             for j in range(nbin):
                 bol1 = Phase < position[j]+winsize
                 bol2 = Phase >= position[j]
                 I = np.nonzero(bol1 & bol2)[0]
                 MeanAmp[j]=np.mean(Amp[I])
             #MI=(np.log(nbin)-(-np.sum((MeanAmp/np.sum(MeanAmp))*np.log((MeanAmp/np.sum(MeanAmp))))))/np.log(nbin)
             MI =np.log(nbin)-(stat.entropy(MeanAmp)/np.log(nbin))
             Comodulogram[idx2,idx1]=MI;
     plt.contourf(PhaseFreqVector+PhaseFreq_BandWidth/2,AmpFreqVector+AmpFreq_BandWidth/2,Comodulogram.T,100)
Esempio n. 26
0
def demodulate(sdr_samples, sample_rate, ignore=0):
    t = np.arange(len(sdr_samples)) / sample_rate
    demod = np.exp(-2j*pi*freq_offset*t)
    y = sdr_samples * demod

    threshold = 0.04
    y = y[abs(y) > threshold]

    from scipy import signal  #should be imported already?

    sig = angle(y[1:] * conj(y[:-1]))

    h = signal.firwin(256,5000.0,nyq=sample_rate/2.0)
    sigf = signal.fftconvolve(sig, h)
    if ignore > 0:
        sigf = sigf[ignore:-ignore]
    
    
    downsample = 24
    sigfd = sigf[::downsample]

    fs_down = fs
    sigfdd = signal.resample(sigfd, len(sigfd) * float(fs_down) / (sample_rate / float(downsample)))
    
    return sigfdd
Esempio n. 27
0
def highpass(CutOffFreq, SamplingRate, StopGain, TranWidth):
    NiquistRate = SamplingRate/2.0
    N, beta = sig.kaiserord(StopGain,TranWidth/NiquistRate)
    print 'the order of the FIR filter is:' + str(N) + ', If this is bigger than the size of the data please adjust the width and gain of the filter'
        
    taps = sig.firwin(N, CutOffFreq, window=('kaiser', beta), pass_zero=False, scale=True, nyq=NiquistRate)   
    return taps
Esempio n. 28
0
def decimate_coeffs(q, n=None, ftype='iir'):

    if type(q) != type(1):
        raise Error, "q should be an integer"

    if n is None:
        if ftype == 'fir':
            n = 30
        else:
            n = 8
            
    if ftype == 'fir':
        coeffs = GlobalVars.decimate_fir_coeffs
        if (n, 1./q) not in coeffs:
            coeffs[n,1./q] = signal.firwin(n+1, 1./q, window='hamming')
        
        b = coeffs[n,1./q]
        return b, [1.], n 

    else:
        coeffs = GlobalVars.decimate_iir_coeffs
        if (n,0.05,0.8/q) not in coeffs:
            coeffs[n,0.05,0.8/q] = signal.cheby1(n, 0.05, 0.8/q)
           
        b, a = coeffs[n,0.05,0.8/q]
        return b, a, n
Esempio n. 29
0
def lowpass(sig):
      sample_rate=200
      # The Nyquist rate of the signal.
      nyq_rate = sample_rate / 2.0
      # The desired width of the transition from pass to stop,
      # relative to the Nyquist rate.  We'll design the filter
      # with a 10 Hz transition width.
      width = 5.0/nyq_rate

      # The desired attenuation in the stop band, in dB.
      ripple_db = 10.0
      # Compute the order and Kaiser parameter for the FIR filter.
      N, beta = kaiserord(ripple_db, width)
      #print "N is " , N
      # The cutoff frequency of the filter.
      cutoff_hz = 15
      # Use firwin with a Kaiser window to create a lowpass FIR filter.
      taps = firwin(N, cutoff_hz/nyq_rate, window=('kaiser', beta))
      # Use lfilter to filter x with the FIR filter.
      
      #print taps
      delay = 0.5 * (N-1) / sample_rate
      print "Phase delay is "  , delay

      return lfilter(taps, 1.0, sig)
Esempio n. 30
0
 def LPmin(self, fil_dict):
     self.get_params(fil_dict)
     (self.N, F, A, W) = pyfda_lib.remezord([self.F_PB, self.F_SB], [1, 0],
         [self.A_PB, self.A_SB], Hz = 1, alg = self.alg)
     fil_dict['F_C'] = (self.F_SB + self.F_PB)/2 # use average of calculated F_PB and F_SB
     self.save(fil_dict, sig.firwin(self.N, fil_dict['F_C'], 
                                    window = self.firWindow, nyq = 0.5))
Esempio n. 31
0
def test_firwin(num_samps, f1, f2):
    cpu_window = signal.firwin(num_samps, [f1, f2], pass_zero=False)
    gpu_window = cp.asnumpy(
        cusignal.firwin(num_samps, [f1, f2], pass_zero=False)
    )
    assert array_equal(cpu_window, gpu_window)
Esempio n. 32
0
 def __init__(self, Fsample = 8000, Fcutt = 1000, Width = 400, \
                  Ripple = 60.0):
     nyq_rate = Fsample / 2.0
     width = Width / nyq_rate
     N, beta = kaiserord(Ripple, width)
     self.taps = firwin(N, Fcutt / nyq_rate, window=('kaiser', beta))
Esempio n. 33
0
import scipy.signal as signal
from scipy.fftpack import fft

filter = [0] * ch_number
tap_number = 61
nyquist_freq = sampling_freq * 0.5
"""
N = extraction_freq
dt = 1 / sampling_freq * 2
freq = np.linspace(0, 1.0/dt, N) # frequency step
yf_1 = fft(data_x[0][0])/(N/2)
"""
#"""
#50Hzノッチフィルター
print("50Hz notch filter")
filter = signal.firwin(numtaps=tap_number, cutoff=[49, 51], fs=sampling_freq)
data_x[0:3, :all_sample_number] = signal.lfilter(
    filter, 1, data_x[0:3, :all_sample_number])
"""
yf_2 = fft(data_x[0][0])/(N/2)

plt.plot(freq,np.abs(yf_1))
plt.show()
plt.plot(freq,np.abs(yf_2))
plt.show()
"""
#"""
#カットオフ周波数0.5Hzのハイパスフィルター
print("High-pass filter with a cutoff frequency of 0.5Hz")
filter = signal.firwin(numtaps=tap_number, cutoff=0.5, fs=sampling_freq)
data_x[0:3, :all_sample_number] = signal.lfilter(
Esempio n. 34
0
def highpass(mono, samplerate):
    b = sg.firwin(101, cutoff=1000, fs=samplerate, pass_zero=False)
    filtered = sg.lfilter(b, [1.0], mono)
    return filtered
Esempio n. 35
0
plt.subplot(212)
plt.plot(signal2)
plt.xlim([0, 400])
plt.ylabel("Signal 2")
plt.show()

# PLOT FOURIER TRANSFORMS ----------------------------

ECG = signal2
fs = 120
ECG = ECG - np.mean(ECG)  # Remove DC component
plotfft(ECG, fs)

# FILTER ---------------------------------------------

h = sig.firwin(101, [5, 40], width=None, window='hamming', pass_zero=False, scale=True, fs=fs)

mfreqz(h,1,fs)   # bode plot

ECG_filtered = sig.fftconvolve(h, ECG)

# before and after filtering
plt.figure()
plotfft(ECG, fs)
plotfft(ECG_filtered, fs)
plt.legend(['Original','Filtered'])
plt.show()


plt.figure()
plt.subplot(211)
Esempio n. 36
0
from scipy.signal import fftconvolve, lfilter, firwin


def conj(c):
    con = c.real - 1j * c.imag
    return con


Fs = 100.0
Ts = 1 / Fs
fc = 20
fd = 25.0
kf = fd / Fs

#Create a FIR filter
b = firwin(2, 0.99, width=0.05, pass_zero=True)
# error en el oscilador del demodulador
demod_fc_error = 0.0


def ind(t):
    return int(round(t * Fs))


time = range(0, int((0.5 - Ts) * Fs), int(round(Ts * Fs)))

TestFreq = 4.0
prev_csum = 0
prev_y = 0 + 0j
msg = [0] * len(time)
v = [0] * len(time)
Esempio n. 37
0
 def __init__(self, filterorder=10, cutoff=0.5):
     self.filter = sig.firwin(filterorder, cutoff)
Esempio n. 38
0
import scipy.signal as sig
from scipy.fftpack import fft, fftshift
from spectrum import CORRELOGRAMPSD
from spectrum.tools import cshift
import scipy.io as sio

def mfreqz(b,a=1):
    w,h = sig.freqz(b,a)
    h_dB = 20 * np.log10 (abs(h))
    plt.subplot(211)
    plt.plot(w/max(w),h_dB)
    plt.ylim(-150, 5)
    plt.ylabel('Magnitude (db)')
    plt.xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
    plt.title(r'Frequency response')
    plt.subplot(212)
    h_Phase = np.unwrap(np.arctan2(np.imag(h),np.real(h)))
    plt.plot(w/max(w),h_Phase)
    plt.ylabel('Phase (radians)')
    plt.xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
    plt.title(r'Phase response')
    plt.subplots_adjust(hspace=0.5)
    
fs = 1000 # Hz
nyq_frec = fs/2

n=500
d = sig.firwin(n, cutoff=[0.3, 0.5], window='blackmanharris', pass_zero=False)

mfreqz(d)
plt.show()
Esempio n. 39
0
def kaiser_LP_filter(traces, fs, cutoff, width, attenuation):
    numtaps, beta = signal.kaiserord(attenuation, width / (0.5 * fs))
    taps = signal.firwin(numtaps, cutoff / (0.5 * fs), window=('kaiser', beta))
    traces_filtered = signal.lfilter(taps, [1.0], traces)
    return traces_filtered
Esempio n. 40
0
def AddLpFilter(config_lines,
                name,
                input,
                rate,
                num_lpfilter_taps,
                lpfilt_filename,
                is_updatable=False):
    try:
        import scipy.signal as signal
        import numpy as np
    except ImportError:
        raise Exception(
            " This recipe cannot be run without scipy."
            " You can install it using the command \n"
            " pip install scipy\n"
            " If you do not have admin access on the machine you are"
            " trying to run this recipe, you can try using"
            " virtualenv")
    # low-pass smoothing of input was specified. so we will add a low-pass filtering layer
    lp_filter = signal.firwin(num_lpfilter_taps,
                              rate,
                              width=None,
                              window='hamming',
                              pass_zero=True,
                              scale=True,
                              nyq=1.0)
    lp_filter = list(np.append(lp_filter, 0))
    nnet3_train_lib.WriteKaldiMatrix(lpfilt_filename, [lp_filter])
    filter_context = int((num_lpfilter_taps - 1) / 2)
    filter_input_splice_indexes = range(-1 * filter_context,
                                        filter_context + 1)
    list = [('Offset({0}, {1})'.format(input['descriptor'], n)
             if n != 0 else input['descriptor'])
            for n in filter_input_splice_indexes]
    filter_input_descriptor = 'Append({0})'.format(' , '.join(list))
    filter_input_descriptor = {
        'descriptor': filter_input_descriptor,
        'dimension': len(filter_input_splice_indexes) * input['dimension']
    }

    input_x_dim = len(filter_input_splice_indexes)
    input_y_dim = input['dimension']
    input_z_dim = 1
    filt_x_dim = len(filter_input_splice_indexes)
    filt_y_dim = 1
    filt_x_step = 1
    filt_y_step = 1
    input_vectorization = 'zyx'

    tdnn_input_descriptor = nodes.AddConvolutionLayer(
        config_lines,
        name,
        filter_input_descriptor,
        input_x_dim,
        input_y_dim,
        input_z_dim,
        filt_x_dim,
        filt_y_dim,
        filt_x_step,
        filt_y_step,
        1,
        input_vectorization,
        filter_bias_file=lpfilt_filename,
        is_updatable=is_updatable)

    return [tdnn_input_descriptor, filter_context, filter_context]
Esempio n. 41
0
def MdPF_calc(Myo_Frames, Midi_Frames):
    MdPF_Frames = []

    rate = 1000  #sampling rate
    nyq = rate / 2  #ナイキスト周波数

    i = 1

    while i <= len(Midi_Frames) - 1:
        start_point = Midi_Frames[i - 1][0] - Midi_Frames[0][0]
        end_point = Midi_Frames[i][0] - Midi_Frames[0][0]

        #high pass fir
        fe = 20.0 / nyq  #[Hz]
        numtaps = 255  #フィルタ係数(タップの数(要奇数))
        co = spsig.firwin(numtaps, fe, pass_zero=False)  #setting coefficient
        high_pass_sig = spsig.lfilter(co, 1, Myo_Frames[start_point:end_point])

        power_spectrum = do_fft(high_pass_sig)
        freqList = fftfreq(high_pass_sig.size, d=1.0 / rate)  #周波数の分解能計算

        # --- 表示周波数帯制限 ---
        #low cut
        List_count_low = 0
        for row in freqList:
            if (row > 20) or (row < 0):  #20Hz以上
                List_count_low -= 1
                break
            List_count_low += 1

        #high cut
        List_count_high = 0
        for row in freqList:
            if (row > 500) or (row < 0):  #500Hz以下
                List_count_high -= 1
                break
            List_count_high += 1

        spectrum_sum = np.sum(power_spectrum[List_count_low:List_count_high])

        find_mid = 0
        index = List_count_low

        while find_mid < spectrum_sum / 2:
            find_mid += power_spectrum[index]
            index += 1
        low_mid_f = index - 1

        MdPF_Frames.append(freqList[low_mid_f])
        """
        find_mid = 0
        index = List_count_high

        while find_mid < spectrum_sum / 2:
            find_mid += power_spectrum[index]
            index -= 1
        hi_mid_f = index + 1

        MdPF_Frames.append((freqList[low_mid_f] + freqList[hi_mid_f]) / 2)
        """

        i += 1
    return MdPF_Frames
Esempio n. 42
0
#example taken from scipy documentation
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np

#disenar el filtro usando una ventana kaiser
b = signal.firwin(32, 0.5, window='hamming', pass_zero=True)
#pasa bajas pass_zero=True
#pasa altas pass_zero=False

fs = 1e3
t, T = np.linspace(1. / fs, 5, fs * 5, retstep=True)
nFs = 1 / T

F = 10  #frecuencia fundamental 10 hz
w = 2 * np.pi * 5  #frecuencia angular
Vm = 4  #valor de amplitud de la onda

#generar onda compuesta de sinusoides
y = Vm * np.cos(
    w * t) + Vm / 2 * np.cos(2 * w * t + np.deg2rad(45)) + Vm / 3 * np.cos(
        3 * w * t) + Vm / 2 * np.cos(4 * w * t)
#generar onda de ruido sinusoidal, alta frecuencia y baja amplitud
x = 2 * np.cos(2 * np.pi * 370 * t)
#onda con ruido
yx = y + x

#filtrar la onda con ruido usando el filtro FIR
yf = signal.lfilter(b, [1.0], yx)

plt.subplot(311)
Esempio n. 43
0
nyq_rate = sample_rate / 2.0

# The desired width of the transition from pass to stop,
# relative to the Nyquist rate.  We'll design the filter
# with a 4 Hz transition width.
width = 4.0 / nyq_rate

# The desired attenuation in the stop band, in dB.
ripple_db = 60.0

# Compute the order and Kaiser parameter for the FIR filter.
N, beta = kaiserord(ripple_db, width)

# The cutoff frequency of the filter.
cutoff_hz = 10.0

# Use firwin with a Kaiser window to create a lowpass FIR filter.
taps = firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta))

# Use lfilter to filter x with the FIR filter.
filtered_x = lfilter(taps, 1.0, x)

figure(1)
plot(times, voltage)
xlabel("Time [s]")
ylabel("Voltage [mV]")
title("Load Cell Voltage v. Time")
grid(True)

show()
Esempio n. 44
0
def xkcd_line(x, y, xlim=None, ylim=None,
              mag=1.0, f1=30, f2=0.05, f3=15):
    """
    Mimic a hand-drawn line from (x, y) data

    Parameters
    ----------
    x, y : array_like
        arrays to be modified
    xlim, ylim : data range
        the assumed plot range for the modification.  If not specified,
        they will be guessed from the  data
    mag : float
        magnitude of distortions
    f1, f2, f3 : int, float, int
        filtering parameters.  f1 gives the size of the window, f2 gives
        the high-frequency cutoff, f3 gives the size of the filter

    Returns
    -------
    x, y : ndarrays
        The modified lines
    """
    x = np.asarray(x)
    y = np.asarray(y)

    # get limits for rescaling
    if xlim is None:
        xlim = (x.min(), x.max())
    if ylim is None:
        ylim = (y.min(), y.max())

    if xlim[1] == xlim[0]:
        xlim = ylim

    if ylim[1] == ylim[0]:
        ylim = xlim

    # scale the data
    x_scaled = (x - xlim[0]) * 1. / (xlim[1] - xlim[0])
    y_scaled = (y - ylim[0]) * 1. / (ylim[1] - ylim[0])

    # compute the total distance along the path
    dx = x_scaled[1:] - x_scaled[:-1]
    dy = y_scaled[1:] - y_scaled[:-1]
    dist_tot = np.sum(np.sqrt(dx * dx + dy * dy))

    # number of interpolated points is proportional to the distance
    Nu = int(200 * dist_tot)
    u = np.arange(-1, Nu + 1) * 1. / (Nu - 1)

    # interpolate curve at sampled points
    k = min(3, len(x) - 1)
    res = interpolate.splprep([x_scaled, y_scaled], s=0, k=k)
    x_int, y_int = interpolate.splev(u, res[0])

    # we'll perturb perpendicular to the drawn line
    dx = x_int[2:] - x_int[:-2]
    dy = y_int[2:] - y_int[:-2]
    dist = np.sqrt(dx * dx + dy * dy)

    # create a filtered perturbation
    coeffs = mag * np.random.normal(0, 0.01, len(x_int) - 2)
    b = signal.firwin(f1, f2 * dist_tot, window=('kaiser', f3))
    response = signal.lfilter(b, 1, coeffs)

    x_int[1:-1] += response * dy / dist
    y_int[1:-1] += response * dx / dist

    # un-scale data
    x_int = x_int[1:-1] * (xlim[1] - xlim[0]) + xlim[0]
    y_int = y_int[1:-1] * (ylim[1] - ylim[0]) + ylim[0]

    return x_int, y_int
Esempio n. 45
0
def bandpass_firwin(ntaps, lowcut, highcut, fs, window='hamming'):
    nyq = 0.5 * fs
    taps = firwin(ntaps, [lowcut, highcut], nyq=nyq, pass_zero=False,
                  window=window, scale=False)
    return taps
Esempio n. 46
0
DCnotchZi2 = np.zeros([8,1])

#Butterworth lowpass filter
N  = 4    # Filter order
fk = 30
Wn = fk/(fs/2) # Cutoff frequency
lowpassB, lowpassA = signal.butter(N, Wn, output='ba')
lowpassZi = np.zeros([8,N])


#FIR bandpass filter
hcc = 56.0/(fs/2)
hc = 44.0/(fs/2) #High cut
lc = 5.0/(fs/2)	#Low cut

bandpassB = signal.firwin(window, [lc, hc], pass_zero=False, window = 'hann') #Bandpass
bandpassA = 1.0 #np.ones(len(bandpassA))
bandpassZi = np.zeros([8, window-1])

highpassB = signal.firwin(window, lc, pass_zero=False, window = 'hann') #Bandpass
highpassA = 1.0
highpassZi = np.zeros([8, window-1])
print("Filtersetup finished")

multibandB = signal.firwin(window, hc, pass_zero=True, window = 'hann') #Bandpass
multibandA = 1.0
multibandZi = np.zeros([8, window-1])

def dataCatcher():
	global board
	board.start_streaming(printData)
dt = 1/F_SAMPLE
time = np.linspace(0, LEN_DATA*dt, LEN_DATA)
time_long = np.linspace(0, 4096*dt, 4096)
freq = np.linspace(0.0, F_SAMPLE//2, LEN_DATA//2)
freq_long = np.linspace(0, F_SAMPLE//2, 4096//2)

data = np.genfromtxt("../accelerometer_calibration/4kHz.txt", delimiter=',')
x_long = np.genfromtxt("../accelerometer_calibration/4kHz_x.txt", delimiter=',')
x_long = Data(x_long)
x_data = Data(data[:, 0])
y_data = Data(data[:, 1])
z_data = Data(data[:, 2])

numtaps = 30

taps = signal.firwin(numtaps, 4/F_SAMPLE, window='blackman')
filtered_x = signal.lfilter(taps, 1.0, x_long.data)

#------------------------------------------------
# Plot the FIR filter coefficients.
#------------------------------------------------

plt.figure(1)
plt.plot(taps, 'bo-', linewidth=2)
plt.title('Filter Coefficients (%d taps)' % numtaps)
plt.grid(True)

#------------------------------------------------
# Plot the magnitude response of the filter.
#------------------------------------------------
Esempio n. 48
0
    def process(self, data, parameters):

        if parameters[0] == 'B':  #Bandpass Filter
            if np.array(parameters.split(',')).size == 4:

                #Estimate Filter coefficients
                parameter = np.array(parameters.split(','))
                p = np.array(parameter)
                n = int(p[3])
                a = signal.firwin(n, np.float(p[1]))
                b = -signal.firwin(n, np.float(p[2]))
                b[n / 2] = b[n / 2] + 1
                d = -(a + b)
                d[n / 2] = d[n / 2] + 1
                w1, FilterCoef = signal.freqz(d)
                FilterCoefLength = FilterCoef.size

                #Filter Data with estimated coefficients
                Data = np.transpose(np.array(data))
                size = Data.shape
                FilteredData = np.zeros((size[0], size[1]))

                for c in range(0, size[0]):
                    FilteredData[c, :] = signal.lfilter(FilterCoef, 1, Data)
                result = pd.DataFrame(np.transpose((FilteredData)))
            else:
                result = np.zeros(1000)
                result = pd.DataFrame(np.transpose((result)))

        if parameters[0] == 'L':  #LowPass Filter
            if np.array(parameters.split(',')).size == 3:

                #Estimate Filter coefficients
                parameter = np.array(parameters.split(','))
                p = np.array(parameter)
                n = int(p[2])
                a = signal.firwin(n, np.float(p[1]))
                w1, FilterCoef = signal.freqz(a)
                FilterCoefLength = FilterCoef.size

                #Filter Data with estimated coefficients
                Data = np.transpose(np.array(data))
                size = Data.shape
                FilteredData = np.zeros((size[0], size[1]))

                for c in range(0, size[0]):
                    FilteredData[c, :] = signal.lfilter(FilterCoef, 1, Data)
                result = pd.DataFrame(np.transpose((FilteredData)))
            else:
                result = np.zeros(1000)
                result = pd.DataFrame(np.transpose((result)))

        if parameters[0] == 'H':  #Highpass Filter
            if np.array(parameters.split(',')).size == 3:

                #Estimate Filter coefficients
                parameter = np.array(parameters.split(','))
                p = np.array(parameter)
                n = int(p[2])
                b = -signal.firwin(n, np.float(p[1]))
                b[n / 2] = b[n / 2] + 1
                w1, FilterCoef = signal.freqz(b)
                FilterCoefLength = FilterCoef.size

                #Filter Data with estimated coefficients
                Data = np.transpose(np.array(data))
                size = Data.shape
                FilteredData = np.zeros((size[0], size[1]))

                for c in range(0, size[0]):
                    FilteredData[c, :] = signal.lfilter(FilterCoef, 1, Data)
                result = pd.DataFrame(np.transpose((FilteredData)))
            else:
                result = np.zeros(1000)
                result = pd.DataFrame(np.transpose((result)))

        return result
Esempio n. 49
0
 def createfilter(self, filterorder, cutoff):
     self.filter = sig.firwin(filterorder, cutoff)
Esempio n. 50
0
    ## FIRWIN: Filterentwurf mit gefensterter (Default: Hamming)
    #          Fourier-Approximation (entspricht fir1 bei Matlab / Octave)
    # scipy.signal.firwin(numtaps, cutoff, width=None, window='hamming',
    #                     pass_zero=True, scale=True, nyq=1.0)
    #
    if FILT_FIR_METHOD == 'WIN':
        #
        # Hier wird die -6 dB Grenzfrequenz ( bezogen auf f_S/2 ) spezifiziert,
        # kein Übergangsbereich wie bei firls -> ungÃŒnstig, da ein don't care -
        # Bereich zwischen f_DB und f_SB nicht gezielt ausgenutzt werden kann!
        # DafÃŒr kann ÃŒber die Auswahl des Fenstertyps "Finetuning" betrieben werden.
        # Mit pass_zero = True wird |H(f=0)| = 1 (Tiefpass, BP),
        # mit pass_zero = False wird |H(f=f_S/2| = 1 (Hochpass, BS) erzwungen.
        # FÃŒr alle Filtertypen definiert der Frequenzvektor F die Eckfrequenzen
        # der DurchlassbÀnder. F=[0.35 0.55] und pass_zero = 1 erzeugt Bandpass
        bb = sig.firwin(L, F_DB * 2.0, window=FILT_FIR_WINDOW)

    elif FILT_FIR_METHOD == 'WIN2':
        #=======================================================================
        ## FIRWIN2: Frequency Sampling FIR-Filterentwurf: Es wird ein linearphas.
        #           Filter erzeugt, das bei den Frequenzen 'freq' die VerstÀrkung
        #           'gain' hat (entsprechend fir2 bei Matlab / Octave)
        # scipy.signal.firwin2(numtaps, freq, gain, nfreqs=None, window='hamming',
        #                           nyq=1.0, antisymmetric=False)
        #
        # Mit antisymmetric = True werden Filter mit ungerader Symmetrie gewÀhlt
        # (Typ III oder IV), je nachdem ob numtaps gerade oder ungerade ist, wird
        # der Typ weiter eingeschrÀnkt.
        bb = sig.firwin2(L, [0, F_DB, F_SB, 1], [1, 1, 0, 0],
                         window=FILT_FIR_WINDOW)
    # Example for Multi-band Hilbert Filter taken from Matlab firls reference
Esempio n. 51
0
class Algorithm:

    # Initial parameters
    Fs = 200  # Data packs come in every 5ms = 200Hz
    wsize1 = 0.15  # MAF (moving average filter) size for Energy Level Detection, size of 1st MAF
    wsize2 = 0.2  # MAF size for Energy Variation Detection, size of 2nd MAF
    refractory_time = 0.15  # Refractory Period
    thEL0 = 0.1  # Initial value for energy level threshold
    stabLevel = 0.5  # Stabilization Reference Voltage
    r_a = 0.1  # application rate for weight adjustment
    r_b = 0.05  # application rate for weight adjustment
    r_nr = 1.75  # application rate of noise level (for signal threshold)
    r_s = 0.001  # application rate of signal level (for noise threshold)
    r_d = 0.05  # decay rate for adaptive threshold
    r_n = 0.03  # application rate of noise level (for signal threshold)
    Weight = 1  # weight for adjustment signal level

    winsizeEL = round(wsize1 * Fs)  # window size for Energy Level (EL)
    winsizeEV = round(wsize2 * Fs)  # window size for Energy Variation (EV)
    diffWinsize = winsizeEV - winsizeEL  # difference in window sizes
    refractoryP = round(refractory_time * Fs)  # refractory period

    thEVlimit = 1 * Fs / (0.2 * Fs * 20)
    thEVub = 0.45 * Fs / (0.2 * Fs * 20)
    thEVlb = -0.45 * Fs / (0.2 * Fs * 20)
    thEVub2 = 20 * Fs / (0.2 * Fs * 20)
    thEVlb2 = -20 * Fs / (0.2 * Fs * 20)

    ArrayL = 5
    decayF = 1 - 1 / ((0.40 - refractory_time) * Fs)
    checker2 = 0
    isStart = 0
    maxV_Buf = None
    maxV = 1
    QRScount = 0

    cutoffs = np.array([
        (5 / (Fs / 2)), (25 / (Fs / 2))
    ])  # Cutoff frequencies for the bandpass filter (5Hz - 25Hz)

    maxVArray = np.zeros((ArrayL, 1))
    maxDifBuf = np.zeros((ArrayL, 1))
    minDifBuf = np.zeros((ArrayL, 1))
    BUF1 = np.zeros((winsizeEL, 1))
    BUF2 = np.zeros((winsizeEV, 1))

    # Expand the following arrays within the iterate array
    ELQRS = np.asarray([])
    EVQRS = np.asarray([])
    thEL = np.asarray([])  # Threshold for EL (Adaptive threshold)
    thEV = np.asarray([])  # Threshold for EV (Hard threshold)
    thN = np.asarray([])
    mem_allocation = 0  # Dummy counter to initialize these arrays

    kk = winsizeEV  # Counter
    # If the signal length gets readjusted each time, is this even necssary? couldn't I just take the last index of the array?
    # maybe set kk = len(data) or whatever

    # Initializes variables that are calculated later within the iterate function
    Timer = -1
    TimerOfPeak = -1
    maxP = -1
    maxP_Buf = -1
    BufStartP2 = -1
    BufEndP2 = -1

    # Window coefficients for the filter in the iterate function
    b = signal.firwin(64, cutoffs, pass_zero=False)

    # Initializes the algorithm object by taking in a lead, which at this stage is more for labelling purposes than anything
    # Also sets up an empty array to store QRS locations in
    def __init__(self, lead):
        self.lead = lead  # Labels the algorithm w/ the corresponding lead
        self.qrsLocs = np.asarray(
            [])  # Stores locations of detected QRS complexes

    # This function runs 1 iteration of the algorithm & is to be called after reading in a single data point.
    # It also saves any detected QRS points in a text file.
    def iterate(self, data, file):
        current_time = time.process_time()

        # Preprocessing, filters the signal
        #fSig = signal.filtfilt(b, [1], data, axis=0)
        fSig = signal.lfilter(self.b, [1], data,
                              axis=0)  # Signal after bandpass filter
        sSig = np.sqrt(fSig**2)  # Signal after squaring
        dSig = self.Fs * np.concatenate(([0], np.diff(sSig, axis=0)),
                                        axis=0)  # Signal after differentiating
        sigLen = len(sSig)

        filter_time = time.process_time() - current_time
        print('Filter time: ' + str(filter_time))
        #print('---')

        #Initializes the arrays, then expands them each iteration after that
        if self.mem_allocation == 0:
            self.ELQRS = np.zeros((sigLen, 1))
            self.EVQRS = np.zeros((sigLen, 1))
            self.thEL = np.ones((sigLen, 1)) * self.thEL0
            self.thEV = np.zeros((sigLen - 1, 1))
            self.thN = np.zeros((sigLen, 1))
            self.mem_allocation = 1
        else:
            self.ELQRS = np.concatenate((self.ELQRS, [[0]]))
            self.EVQRS = np.concatenate((self.EVQRS, [[0]]))
            self.thEL = np.concatenate((self.thEL, [[self.thEL0]]))
            self.thEV = np.concatenate((self.thEV, [[0]]))
            self.thN = np.concatenate((self.thN, [[0]]))

        LargeWin = self.winsizeEV

        ### Moving average w/ weight ###
        if self.kk == 193:
            #if self.kk == LargeWin:
            for i in np.arange(len(self.BUF1), self.kk - 1):
                self.BUF1 = np.concatenate((self.BUF1, [[0]]), axis=0)
        self.BUF1 = np.concatenate((self.BUF1, [[
            (np.sum(sSig[self.kk - self.winsizeEL:self.kk], axis=0) /
             self.winsizeEL)
        ]]),
                                   axis=0)
        self.BUF2 = np.concatenate((self.BUF2, [[
            (np.sum(dSig[self.kk - self.winsizeEV:self.kk], axis=0) /
             self.winsizeEV)
        ]]),
                                   axis=0)
        self.ELQRS[self.kk - 1] = np.sum(np.copy(
            self.BUF1[self.kk - self.winsizeEL:self.kk]),
                                         axis=0) / self.winsizeEL
        self.EVQRS[self.kk - 1] = np.sum(np.copy(
            self.BUF2[self.kk - self.winsizeEV:self.kk]),
                                         axis=0) / self.winsizeEV

        ### Step 1: Energy Level Detection ###
        if self.isStart == 0 and self.ELQRS[self.kk - 1] >= self.thEL[self.kk -
                                                                      1]:
            self.thEL[self.kk - 1] = np.copy(self.ELQRS[self.kk - 1])
            self.maxV = np.copy(self.ELQRS[self.kk - 1])
            self.maxP = self.kk - 1
            self.isStart = 1
        if self.ELQRS[self.kk - 1] < self.thN[self.kk - 1]:
            self.thN[self.kk - 1] = np.copy(self.ELQRS[self.kk - 1])

        if self.isStart == 1:
            if self.ELQRS[self.kk - 1] >= self.maxV:
                self.thEL[self.kk - 1] = np.copy(self.ELQRS[self.kk - 1])
                self.maxV = np.copy(self.ELQRS[self.kk - 1])
                self.maxP = self.kk - 1
                self.Timer = self.refractoryP
            else:
                self.Timer = self.Timer - 1
                self.thEL[self.kk - 1] = self.maxV
                if self.Timer == 0:
                    self.isStart = 0
                    self.checker2 = 1
                    self.TimerOfPeak = self.winsizeEV - (self.refractoryP -
                                                         self.winsizeEL)
                    self.maxP_Buf = self.maxP
                    self.maxV_Buf = self.maxV

        ### Step 2: Energy Variation Detection ###
        if self.checker2 == 1:
            self.TimerOfPeak = self.TimerOfPeak - 1
            if self.TimerOfPeak == 0:
                self.checker2 = 0
                if self.maxP_Buf - self.winsizeEL < 1:
                    self.BufStartP2 = 1
                else:
                    self.BufStartP2 = self.maxP_Buf - self.winsizeEL
                if self.maxP_Buf + 2 * self.diffWinsize > sigLen:
                    self.BufEndP2 = data.size
                else:
                    self.BufEndP2 = self.maxP_Buf + 2 * self.diffWinsize * 2
                DiffSumCheck1 = np.amax(np.copy(
                    self.EVQRS[(self.BufStartP2 - 1):(self.maxP_Buf +
                                                      self.diffWinsize)]),
                                        axis=0)
                DiffSumCheck2 = np.amin(np.copy(
                    self.EVQRS[(self.maxP_Buf + self.diffWinsize -
                                1):self.BufEndP2]),
                                        axis=0)
                if self.qrsLocs.size == 0 or (
                        DiffSumCheck1 - DiffSumCheck2 > self.thEVlimit
                        and DiffSumCheck1 * DiffSumCheck2 < 0
                        and DiffSumCheck1 > self.thEVub
                        and DiffSumCheck2 < self.thEVlb
                        and DiffSumCheck1 < self.thEVub2
                        and DiffSumCheck2 > self.thEVlb2):
                    self.QRScount = self.QRScount + 1
                    self.qrsLocs = np.concatenate(
                        (self.qrsLocs,
                         np.copy([self.maxP_Buf - self.winsizeEL + 2])),
                        axis=0)
                    file.write(
                        str(np.copy([self.maxP_Buf - self.winsizeEL + 2])) +
                        '\n')

                    ### Step 3: Weight Adjustment ###
                    self.maxVArray[(self.QRScount %
                                    self.ArrayL)] = self.maxV_Buf
                    self.maxDifBuf[(self.QRScount % self.ArrayL)] = np.amax(
                        np.copy(self.EVQRS[(self.BufStartP2 -
                                            1):self.BufEndP2]),
                        axis=0)
                    self.minDifBuf[(self.QRScount % self.ArrayL)] = np.amin(
                        np.copy(self.EVQRS[(self.BufStartP2 -
                                            1):self.BufEndP2]),
                        axis=0)
                    if self.stabLevel > np.mean(self.maxVArray, axis=0):
                        AdujR1 = np.amin([
                            self.r_a *
                            (self.stabLevel -
                             np.median(np.copy(self.maxVArray), axis=0)),
                            self.r_b * self.stabLevel
                        ],
                                         axis=0)
                    else:
                        AdujR1 = np.amin([
                            self.r_a *
                            (self.stabLevel -
                             np.median(np.copy(self.maxVArray), axis=0)),
                            -1 * self.r_b * self.stabLevel
                        ],
                                         axis=0)
                    self.Weight = self.Weight + AdujR1
        self.thN[self.kk] = np.copy(
            self.thN[self.kk -
                     1]) + self.r_s * np.copy(self.ELQRS[self.kk - 1])
        if self.maxV_Buf is not None:
            self.thEL[self.kk] = np.copy(self.thEL[self.kk - 1]) * (
                self.decayF *
                (1 - self.r_d *
                 (np.copy(self.thN[self.kk - 1]) / self.maxV_Buf))
            ) + self.r_n * np.copy(self.thN[self.kk - 1])
        else:
            self.thEL[self.kk] = np.copy(self.thEL[self.kk - 1]) * self.decayF
        if self.thEL[self.kk] < self.r_nr * self.thN[self.kk - 1]:
            self.thEL[self.kk] = self.r_nr * np.copy(self.thN[self.kk - 1])

        self.kk = self.kk + 1  #is this necessary?
Esempio n. 52
0
    def run_temporal(self, checkpoint_dir, vid_dir, frame_ext, out_dir,
                     amplification_factor, fl, fh, fs, n_filter_tap,
                     filter_type):
        """Magnify video with a temporal filter.
        Args:
            checkpoint_dir: checkpoint directory.
            vid_dir: directory containing video frames videos are processed
                in sorted order.
            out_dir: directory to place output frames and resulting video.
            amplification_factor: the amplification factor,
                with 0 being no change.
            fl: low cutoff frequency.
            fh: high cutoff frequency.
            fs: sampling rate of the video.
            n_filter_tap: number of filter tap to use.
            filter_type: Type of filter to use. Can be one of "fir",
                "butter", or "differenceOfIIR". For "differenceOfIIR",
                fl and fh specifies rl and rh coefficients as in Wadhwa et al.
        """

        nyq = fs / 2.0
        if filter_type == 'fir':
            filter_b = firwin(n_filter_tap, [fl, fh], nyq=nyq, pass_zero=False)
            filter_a = []
        elif filter_type == 'butter':
            filter_b, filter_a = butter(n_filter_tap, [fl / nyq, fh / nyq],
                                        btype='bandpass')
            filter_a = filter_a[1:]
        elif filter_type == 'differenceOfIIR':
            # This is a copy of what Neal did. Number of taps are ignored.
            # Treat fl and fh as rl and rh as in Wadhwa's code.
            # Write down the difference of difference equation in Fourier
            # domain to proof this:
            filter_b = [fh - fl, fl - fh]
            filter_a = [-1.0 * (2.0 - fh - fl), (1.0 - fl) * (1.0 - fh)]
        else:
            raise ValueError('Filter type must be either '
                             '["fir", "butter", "differenceOfIIR"] got ' + \
                             filter_type)
        head, tail = os.path.split(out_dir)
        tail = tail + '_fl{}_fh{}_fs{}_n{}_{}'.format(fl, fh, fs, n_filter_tap,
                                                      filter_type)
        out_dir = os.path.join(head, tail)
        vid_name = os.path.basename(out_dir)
        # make folder
        mkdir(out_dir)
        vid_frames = sorted(glob(os.path.join(vid_dir, '*.' + frame_ext)))
        first_frame = vid_frames[0]
        im = imread(first_frame)
        image_height, image_width = im.shape
        if not self.is_graph_built:
            self.image_width = image_width
            self.image_height = image_height
            # Figure out image dimension
            self._build_IIR_filtering_graphs()
            ginit_op = tf.global_variables_initializer()
            linit_op = tf.local_variables_initializer()
            self.sess.run([ginit_op, linit_op])

            if self.load(checkpoint_dir):
                print("[*] Load Success")
            else:
                raise RuntimeError('MagNet: Failed to load checkpoint file.')
            self.is_graph_built = True
        try:
            i = int(self.ckpt_name.split('-')[-1])
            print("Iteration number is {:d}".format(i))
            vid_name = vid_name + '_' + str(i)
        except:
            print("Cannot get iteration number")

        if len(filter_a) is not 0:
            x_state = []
            y_state = []

            for frame in tqdm(vid_frames, desc='Applying IIR'):
                file_name = os.path.basename(frame)
                frame_no, _ = os.path.splitext(file_name)
                frame_no = int(frame_no)
                in_frames = [
                    load_train_data([frame, frame, frame],
                                    gray_scale=self.n_channels == 1,
                                    is_testing=True)
                ]
                in_frames = np.array(in_frames).astype(np.float32)

                texture_enc, x = self.sess.run(
                    [self.texture_enc, self.shape_rep],
                    feed_dict={
                        self.input_image: in_frames[:, :, :, :3],
                    })
                x_state.insert(0, x)
                # set up initial condition.
                while len(x_state) < len(filter_b):
                    x_state.insert(0, x)
                if len(x_state) > len(filter_b):
                    x_state = x_state[:len(filter_b)]
                y = np.zeros_like(x)
                for i in range(len(x_state)):
                    y += x_state[i] * filter_b[i]
                for i in range(len(y_state)):
                    y -= y_state[i] * filter_a[i]
                # update y state
                y_state.insert(0, y)
                if len(y_state) > len(filter_a):
                    y_state = y_state[:len(filter_a)]

                out_amp = self.sess.run(self.output_image,
                                        feed_dict={
                                            self.out_texture_enc:
                                            texture_enc,
                                            self.filtered_enc:
                                            y,
                                            self.ref_shape_enc:
                                            x,
                                            self.amplification_factor:
                                            [amplification_factor]
                                        })

                im_path = os.path.join(out_dir, file_name)
                out_amp = np.squeeze(out_amp)
                out_amp = (127.5 * (out_amp + 1)).astype('uint8')
                cv2.imwrite(im_path,
                            cv2.cvtColor(out_amp, code=cv2.COLOR_RGB2BGR))
        else:
            # This does FIR in fourier domain. Equivalent to cyclic
            # convolution.
            x_state = None
            for i, frame in tqdm(enumerate(vid_frames),
                                 desc='Getting encoding'):
                file_name = os.path.basename(frame)
                in_frames = [
                    load_train_data([frame, frame, frame],
                                    gray_scale=self.n_channels == 1,
                                    is_testing=True)
                ]
                in_frames = np.array(in_frames).astype(np.float32)

                texture_enc, x = self.sess.run(
                    [self.texture_enc, self.shape_rep],
                    feed_dict={
                        self.input_image: in_frames[:, :, :, :3],
                    })
                if x_state is None:
                    x_state = np.zeros(x.shape + (len(vid_frames), ),
                                       dtype='float32')
                x_state[:, :, :, :, i] = x

            filter_fft = np.fft.fft(np.fft.ifftshift(filter_b),
                                    n=x_state.shape[-1])
            # Filtering
            for i in trange(x_state.shape[1], desc="Applying FIR filter"):
                x_fft = np.fft.fft(x_state[:, i, :, :], axis=-1)
                x_fft *= filter_fft[np.newaxis, np.newaxis, np.newaxis, :]
                x_state[:, i, :, :] = np.fft.ifft(x_fft)

            for i, frame in tqdm(enumerate(vid_frames), desc='Decoding'):
                file_name = os.path.basename(frame)
                frame_no, _ = os.path.splitext(file_name)
                frame_no = int(frame_no)
                in_frames = [
                    load_train_data([frame, frame, frame],
                                    gray_scale=self.n_channels == 1,
                                    is_testing=True)
                ]
                in_frames = np.array(in_frames).astype(np.float32)
                texture_enc, _ = self.sess.run(
                    [self.texture_enc, self.shape_rep],
                    feed_dict={
                        self.input_image: in_frames[:, :, :, :3],
                    })
                out_amp = self.sess.run(self.output_image,
                                        feed_dict={
                                            self.out_texture_enc:
                                            texture_enc,
                                            self.filtered_enc:
                                            x_state[:, :, :, :, i],
                                            self.ref_shape_enc:
                                            x,
                                            self.amplification_factor:
                                            [amplification_factor]
                                        })

                im_path = os.path.join(out_dir, file_name)
                out_amp = np.squeeze(out_amp)
                out_amp = (127.5 * (out_amp + 1)).astype('uint8')
                cv2.imwrite(im_path,
                            cv2.cvtColor(out_amp, code=cv2.COLOR_RGB2BGR))
            del x_state

        # Try to combine it into a video
        call([
            DEFAULT_VIDEO_CONVERTER, '-y', '-f', 'image2', '-r', '30', '-i',
            os.path.join(out_dir, '%06d.png'), '-c:v', 'libx264',
            os.path.join(out_dir, vid_name + '.mp4')
        ])
Esempio n. 53
0
def LP_filter_preprocess_signal(signal_data, T, num_samples, f_s):
    nyq_rate = f_s / 2.0

    # The desired width of the transition from pass to stop, relative to the Nyquist rate.  We'll design the filter
    # with a 5 Hz transition width.
    # width = 5.0 / nyq_rate # FIXME: large the (5.) distortion span is less !
    width = 100.0 / nyq_rate  # GOOD; but smoothing is higher
    # width = 20.0 / nyq_rate
    # The desired attenuation in the stop band, in dB.
    ripple_db = 60.0
    # Compute the order and Kaiser parameter for the FIR filter.
    N, beta = kaiserord(ripple_db, width)
    # The cutoff frequency of the filter.
    cutoff_hz = 60.0
    # Use firwin with a Kaiser window to create a lowpass FIR filter.
    taps = firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta))
    # Use lfilter to filter x with the FIR filter.
    filtered_x = lfilter(taps, 1.0, signal_data)
    # ------------------------------------------------
    # Plot the FIR filter coefficients.
    # ------------------------------------------------
    if DO_PLOT and not DO_DISABLE:
        plt.figure(3)
        plt.plot(taps, 'bo-', linewidth=2)
        plt.title('Filter Coefficients (%d taps)' % N)
        plt.grid(True)
        plt.savefig("FIR_filter_coefficients.png")
    # ------------------------------------------------
    # Plot the magnitude response of the filter.
    # ------------------------------------------------
    w, h = freqz(taps, worN=8000)
    if DO_PLOT and not DO_DISABLE:
        plt.figure(4)
        plt.clf()
        plt.plot((w / np.pi) * nyq_rate, np.absolute(h), linewidth=2)
        plt.xlabel('Frequency (Hz)')
        plt.ylabel('Gain')
        plt.title('Frequency Response')
        plt.ylim(-0.05, 1.05)
        plt.grid(True)

    # Upper inset plot.
    if DO_PLOT and not DO_DISABLE:
        ax1 = plt.axes([0.42, 0.6, .45, .25])
        plt.plot((w / np.pi) * nyq_rate, np.absolute(h), linewidth=2)
        plt.xlim(0, 8.0)
        plt.ylim(0.9985, 1.001)
        plt.grid(True)

    # Lower inset plot
    if DO_PLOT and not DO_DISABLE:
        ax2 = plt.axes([0.42, 0.25, .45, .25])
        plt.plot((w / np.pi) * nyq_rate, np.absolute(h), linewidth=2)
        plt.xlim(12.0, 20.0)
        plt.ylim(0.0, 0.0025)
        plt.grid(True)
        plt.savefig("FIR_filter_magnitude_response.png")
    # ------------------------------------------------
    # Plot the original and filtered signals.
    # ------------------------------------------------
    # The phase delay of the filtered signal.
    delay = 0.5 * (N - 1) / f_s

    t = np.linspace(0, 1, num_samples)
    front_pad = np.zeros(int(np.floor((N - 1) / 2)))
    back_pad = np.zeros(int(np.ceil((N - 1) / 2)))
    processed_sig = np.append(np.append(front_pad, filtered_x[N - 1:]),
                              back_pad)
    if DO_PLOT:
        plt.figure(5)
        # Plot the original signal.
        plt.plot(t, signal_data)
        # Plot the filtered signal, shifted to compensate for the phase delay.
        # plt.plot(t - delay, filtered_x, 'r-') # FIXME:
        # Plot just the "good" part of the filtered signal.  The first N-1
        # samples are "corrupted" by the initial conditions.
        # plt.plot(t[N - 1:] - delay, filtered_x[N - 1:], 'g', linewidth=2) # FIXME:
        plt.xlabel('t')
        plt.grid(True)
        plt.plot(t, processed_sig, 'g', linewidth=2)
        plt.savefig("FIR_LP_filtered_signal.png")
        plt.show()

    return processed_sig, T, N, f_s
Esempio n. 54
0
def stereo_fm(x, fs=2.4e6, file_name='test.wav'):
    """
    Stereo demod from complex baseband at sampling rate fs.
    Assume fs is 2400 ksps

    Mark Wickert July 2017
    """
    N1 = 10
    b = signal.firwin(64, 2 * 200e3 / float(fs))
    # Filter and decimate (should be polyphase)
    y = signal.lfilter(b, 1, x)
    z = ss.downsample(y, N1)
    # Apply complex baseband discriminator
    z_bb = discrim(z)
    # Work with the (3) stereo multiplex signals:
    # Begin by designing a lowpass filter for L+R and DSP demoded (L-R)
    # (fc = 12 KHz)
    b12 = signal.firwin(128, 2 * 12e3 / (float(fs) / N1))
    # The L + R term is at baseband, we just lowpass filter to remove 
    # other terms above 12 kHz.
    y_lpr = signal.lfilter(b12, 1, z_bb)
    b19 = signal.firwin(128, 2 * 1e3 * np.array([19 - 5, 19 + 5]) / (float(fs) / N1),
                        pass_zero=False);
    z_bb19 = signal.lfilter(b19, 1, z_bb)
    # Lock PLL to 19 kHz pilot
    # A type 2 loop with bandwidth Bn = 10 Hz and damping zeta = 0.707 
    # The VCO quiescent frequency is set to 19000 Hz.
    theta, phi_error = pilot_pll(z_bb19, 19000, fs / N1, 2, 10, 0.707)
    # Coherently demodulate the L - R subcarrier at 38 kHz.
    # theta is the PLL output phase at 19 kHz, so to double multiply 
    # by 2 and wrap with cos() or sin().
    # First bandpass filter
    b38 = signal.firwin(128, 2 * 1e3 * np.array([38 - 5, 38 + 5]) / (float(fs) / N1),
                        pass_zero=False);
    x_lmr = signal.lfilter(b38, 1, z_bb)
    # Coherently demodulate using the PLL output phase
    x_lmr = 2 * np.sqrt(2) * np.cos(2 * theta) * x_lmr
    # Lowpass at 12 kHz to recover the desired DSB demod term
    y_lmr = signal.lfilter(b12, 1, x_lmr)
    # Matrix the y_lmr and y_lpr for form right and left channels:
    y_left = y_lpr + y_lmr
    y_right = y_lpr - y_lmr

    # Decimate by N2 (nominally 5)
    N2 = 5
    fs2 = float(fs) / (N1 * N2)  # (nominally 48 ksps)
    y_left_DN2 = ss.downsample(y_left, N2)
    y_right_DN2 = ss.downsample(y_right, N2)
    # Deemphasize with 75 us time constant to 'undo' the preemphasis 
    # applied at the transmitter in broadcast FM.
    # A 1-pole digital lowpass works well here.
    a_de = np.exp(-2.1 * 1e3 * 2 * np.pi / fs2)
    z_left = signal.lfilter([1 - a_de], [1, -a_de], y_left_DN2)
    z_right = signal.lfilter([1 - a_de], [1, -a_de], y_right_DN2)
    # Place left and righ channels as side-by-side columns in a 2D array
    z_out = np.hstack((np.array([z_left]).T, (np.array([z_right]).T)))

    ss.to_wav(file_name, 48000, z_out / 2)
    print('Done!')
    # return z_bb, z_out
    return z_bb, theta, y_lpr, y_lmr, z_out
Esempio n. 55
0
    def __init__(self,
                 fs=48000.0,
                 Abuffer=1024,
                 Nchunks=43,
                 baud=2400,
                 mark_f=1200,
                 space_f=2400):

        #  Implementation of an afsk1200 TNC.
        #
        #  The TNC processes a `Abuffer` long buffers, till `Nchunks` number of buffers are collected into a large one.
        #  This is because python is able to more efficiently process larger buffers than smaller ones.
        #  Then, the resulting large buffer is demodulated, sampled and packets extracted.
        #
        # Inputs:
        #    fs  - sampling rate
        #   TBW  -  TBW of the demodulator filters
        #   Abuffer - Input audio buffers from Pyaudio
        #   Nchunks - Number of audio buffers to collect before processing
        #   plla    - agressivness parameter of the PLL

        ## compute sizes based on inputs
        self.baud = baud
        self.mark_f = mark_f
        self.space_f = space_f
        self.TBW = 2.0  # TBW for the demod filters
        self.N = (int(fs / baud * self.TBW) //
                  2) * 2 + 1  # length of the filters for demod
        self.fs = fs  # sampling rate
        self.BW = self.TBW / (1.0 * self.N / fs)  # BW of filter based on TBW
        self.Abuffer = Abuffer  # size of audio buffer
        self.Nchunks = Nchunks  # number of audio buffers to collect
        self.Nbuffer = Abuffer * Nchunks + self.N * 3 - 3  # length of the large buffer for processing
        self.Ns = 1.0 * fs / baud  # samples per symbol

        ## state variables for the modulator
        self.prev_ph = 0  # previous phase to maintain continuous phase when recalling the function

        ##  Generate Filters for the demodulator
        self.h_lp = signal.firwin(self.N, self.BW / fs * 1.0, window='hanning')
        self.h_lpp = signal.firwin(self.N,
                                   self.BW * 2 * 1.2 / fs,
                                   window='hanning')
        self.h_space = self.h_lp * exp(
            1j * 2 * pi * (self.space_f) * r_[-self.N / 2:self.N / 2] / fs)
        self.h_mark = self.h_lp * exp(
            1j * 2 * pi * (self.mark_f) * r_[-self.N / 2:self.N / 2] / fs)

        fc = (self.space_f + self.mark_f) / 2
        self.h_bp = signal.firwin(
            self.N, self.BW / fs * 2.2, window='hanning') * exp(
                1j * 2 * pi * fc * r_[-self.N / 2:self.N / 2] / fs)

        ## PLL state variables  -- so conntinuity between buffers is preserved
        self.dpll = np.round(2.0**32 / self.Ns).astype(int32)  # PLL step
        self.pll = 0  # PLL counter
        self.ppll = -self.dpll  # PLL counter previous value -- to detect overflow
        self.plla = 0.3  # PLL agressivness (small more agressive)

        ## state variable to NRZI2NRZ
        self.NRZIprevBit = True

        ## State variables for findPackets
        self.state = 'search'  # state variable:  'search' or 'pkt'
        self.pktcounter = 0  # counts the length of a packet
        self.packet = bitarray.bitarray([0, 1, 1, 1, 1, 1, 1,
                                         0])  # current packet being collected
        self.bitpointer = 0  # poiter to advance the search beyond what was already searched in the previous buffer

        ## State variables for processBuffer
        self.buff = zeros(self.Nbuffer)  # large overlapp-save buffer
        self.chunk_count = 0  # chunk counter
        self.oldbits = bitarray.bitarray([
            0, 0, 0, 0, 0, 0, 0
        ])  # bits from end of prev buffer to be copied to beginning of new
        self.Npackets = 0  # packet counter
Esempio n. 56
0
def Fast_Kurtogram(x, nlevel, Fs=1, opt1=None, opt2=None):
    # Fast_Kurtogram(x,nlevel,Fs)
    # Computes the fast kurtogram of signal x up to level 'nlevel'
    # Maximum number of decomposition levels is log2(length(x)), but it is
    # recommed to stay by a factor 1/8 below this.
    # Fs = sampling frequency of signal x (default is Fs = 1)
    # opt1 = 1: classical kurtosis based on 4th order statistics
    # opt1 = 2: robust kurtosis based on 2nd order statistics of the envelope
    # (if there is any difference in the kurtogram between the two measures, this is
    # due to the presence of impulsive additive noise)
    # opt2 = 1: the kurtogram is computed via a fast decimated filterbank tree
    # opt2 = 2: the kurtogram is computed via the short-time Fourier transform
    # (option 1 is faster and has more flexibility than option 2 in the design of the
    # analysis filter: a short filter in option 1 gives virtually the same results as option 2)
    #
    # -------------------
    # J. Antoni : 02/2005
    # Translation to Python: T. Lecocq 02/2012
    # -------------------
    N = len(x)
    N2 = np.log2(N) - 7
    if nlevel > N2:
        logging.error('Please enter a smaller number of decomposition levels')

    if opt2 is None:
        #~ opt2 = int(raw_input('Choose the kurtosis measure (classic = 1  robust = 2): '))
        opt2 = 1
    if opt1 is None:
        #~ opt1  = int(raw_input('Choose the algorithm (filterbank = 1  stft-based = 2): '))
        opt1 = 1
    # Fast computation of the kurtogram
    ####################################

    if opt1 == 1:
        # 1) Filterbank-based kurtogram
        ############################
        # Analytic generating filters
        N = 16
        fc = .4  # a short filter is just good enough!
        h = si.firwin(N + 1, fc) * np.exp(
            2 * 1j * np.pi * np.arange(N + 1) * 0.125)
        n = np.arange(2, N + 2)
        print(n)
        g = h[(1 - n) % N] * (-1.)**(1 - n)
        # g = np.power(h[(1-n)%N]*(-1), (1-n))

        N = int(np.fix((3. / 2. * N)))
        print(N)
        h1 = si.firwin(N + 1, 2. / 3 * fc) * np.exp(
            2j * np.pi * np.arange(N + 1) * 0.25 / 3.)
        #~ plt.plot(h1)
        #~ plt.show()
        h2 = h1 * np.exp(2j * np.pi * np.arange(N + 1) / 6.)
        h3 = h1 * np.exp(2j * np.pi * np.arange(N + 1) / 3.)

        if opt2 == 1:
            Kwav = K_wpQ(x, h, g, h1, h2, h3, nlevel,
                         'kurt2')  # kurtosis of the complex envelope
        #~ else:
        #~ Kwav = K_wpQ(x,h,g,h1,h2,h3,nlevel,'kurt1')				# variance of the envelope magnitude

        # keep positive values only!
        Kwav[Kwav <= 0] = 0
        fig = plt.figure()

        #~ plt.subplot(ratio='auto')
        Level_w = np.arange(1, nlevel + 1)
        Level_w = np.array([Level_w, Level_w + np.log2(3.) - 1])
        Level_w = sorted(Level_w.ravel())
        Level_w = np.append(0, Level_w[0:2 * nlevel - 1])
        freq_w = Fs * (np.arange(0, 3 * 2.0**nlevel - 1 + 1)) / (
            3 * 2**(nlevel + 1)) + 1.0 / (3. * 2.**(2 + nlevel))

        Kwav_loc = []
        n_crit = len(Kwav) - 2
        Kwav_loc.append(list(Kwav[n_crit]))
        for i in range(len(Kwav)):
            if i != n_crit:
                Kwav_loc.append(list(Kwav[i]))

        # print(type(Kwav_loc))
        # print(type(Kwav))
        Kwav_loc = np.array(Kwav_loc)
        print(Kwav_loc)
        print(Kwav)
        # print(type(Kwav_loc[0]))
        # print(type(Kwav[0]))
        # print(type(Kwav_loc[0][0]))
        # print(type(Kwav[0][0]))
        # sys.exit()
        plt.imshow(Kwav_loc,
                   aspect='auto',
                   extent=(freq_w[0], freq_w[-1], Level_w[-1], Level_w[0]),
                   interpolation='none')
        # plt.imshow(Kwav,aspect='auto',extent=(freq_w[0],freq_w[-1],Level_w[0],Level_w[-1]),interpolation='bilinear')
        # print(Kwav)
        index = np.argmax(Kwav_loc)
        maxima_kurtosis = np.max(Kwav_loc)
        index = np.unravel_index(index, Kwav_loc.shape)
        f1 = freq_w[index[1]]
        # l1 = Level_w[index[0]+1]
        l1 = Level_w[index[0]]
        fi = (index[1]) / 3. / 2**(nlevel + 1)
        fi += 2.**(-2 - l1)
        print(fi, l1, Fs * fi)
        print('+++Max Kurtosis: ', maxima_kurtosis)
        print('+++Center Freq: ', Fs * fi)
        print('+++Level: ', l1)
        print('+++Width Freq: ', Fs / 2.**(l1 + 1))
        print('+++Lowpass Freq: ', Fs * fi - (Fs / 2.**(l1 + 1)) / 2.)
        print('+++Highpass Freq: ', Fs * fi + (Fs / 2.**(l1 + 1)) / 2.)
        plt.colorbar()
        plt.show()
    else:
        logging.error('stft-based is not implemented')

    #Ajouter le signal filtering !
    c = []
    #~ test = int(raw_input('Do you want to filter out transient signals from the kurtogram (yes = 1 ; no = 0): '))
    test = 1
    #~ fi = fi * Fs
    lev = l1
    while test == 1:
        #~ fi = float(input(['	Enter the optimal carrier frequency (btw 0 and ',num2str(Fs/2),') where to filter the signal: ']));
        #~ fi = fi/Fs;
        #~ if opt1 == 1:
        #~ lev = input(['	Enter the optimal level (btw 0 and ',num2str(nlevel),') where to filter the signal: ']);
        if opt2 == 1:
            c, Bw, fc = Find_wav_kurt(x, h, g, h1, h2, h3, nlevel, lev, fi,
                                      'kurt2', Fs)
        #~ else
        #~ [c,Bw,fc] = Find_wav_kurt(x,h,g,h1,h2,h3,nlevel,lev,fi,'kurt1',Fs);
        test = int(
            raw_input(
                'Do you want to keep on filtering out transients (yes = 1 ; no = 0): '
            ))
Esempio n. 57
0
def firfedge(x, f_range, fs=1000, w=3):
    nyq = np.float(fs / 2)
    Ntaps = np.floor(w * fs / f_range[0])
    taps = scsig.firwin(Ntaps, np.array(f_range) / nyq, pass_zero=False)
    return scsig.filtfilt(taps, [1], x)
Esempio n. 58
0
import numpy as np
from scipy import fftpack
from scipy import signal
import matplotlib.pyplot as plt
from matplotlib import mlab

n = 1024*4
fs = 512
dt = 1/fs

b = signal.firwin(30, 0.2, window="boxcar")
x = np.random.randn(n)
y = signal.filtfilt(b, 1, x)

w, h = signal.freqz(b, 1, fs)

plt.plot(w*fs/(2*np.pi), 20*np.log10(np.abs(h)), "b")
plt.xlim(0, fs/2)
plt.ylim(-120, 0)
plt.xlabel("Frequency[Hz]")
plt.ylabel("Amplitude[dB]")
plt.show()
Esempio n. 59
0
    idNumBin = BinArray(idNum, nBits)
    txBin = idNumBin

    preludeNBits = 16
    # Add random binary data to ensure time for clock to lock before data bits
    prelude = np.array(np.random.randint(2, size=preludeNBits), dtype="bool")
    txBin = np.append(prelude, txBin)

    carrierFreq = 1 / 32
    nBits = 24  # Add differential encoding bit
    bitPeriod = 128

    # FIR Filter Design
    nTaps = 128
    cuttOffFrequency = 0.005
    b1 = signal.firwin(nTaps, cuttOffFrequency)
    w1, h1 = signal.freqz(b1)

    # Create matched filter by reversing FIR response
    b1 = np.flip(b1)

    print("Unmodulated TX Data: ")
    print(txBin)

    # Differnetial Encoding
    #difEncoded = DifferentialEncoder(txBin)

    # Modulate Signal Using BPSK scheme
    txBPSK = ModulationBPSK(txBin, bitPeriod, carrierFreq)

    #difEncoded = DifferentialEncoder(txBin)
Esempio n. 60
0
        int(neuronID) for x in eventContent
        if populationID == x[1] and neuronID == x[2] and x[3] == '0'
    ]
    plt.eventplot(x, lineoffsets=int(neuronID), linelengths=0.7)
    # plt.scatter(x, y)


plotValueOutputs('999', '0', '3', 'signal input')
# plotValueOutputs('1', '0', '3')
plotValueOutputs('321', '0', '3', 'decode output')
plotValueOutputs('1234', '0', '3', 'ann output')
# plotSpikeOutputs('5678', '0')

plotSpikeOutputs('93876079284464', '0')
# plotValueOutputs('94261706957552', '0', '0')

for i in range(1000):
    # plt.axvline(x=0.1*i, color='r', linewidth=0.5)
    pass

t = signal.firwin(24, 5, fs=100)
# plt.plot(t)

filt = ""
for s in t:
    filt = filt + str(s) + ","
# print(filt)

plt.legend()
plt.show()