def fir_sim(impl, t_b, seq, do_cosim, target='build/fir'): # get 'b' factors b = firwin(8, [0.05, 0.95], width=0.05, pass_zero=False) b_fixp = [t_b(i) for i in b] # get result res = np.convolve(seq, b) # saturate the results value to filter output type if needed for i, r in enumerate(res): res[i] = fixp_sat(t_b, r) # driving drv(t=t_b, seq=seq) \ | impl(b=b_fixp) \ | Float \ | check(ref=res[:len(seq)], cmp=fir_compare) # optionally generate HDL code do co-simulation in verilator if do_cosim: cosim(f'{impl}', 'verilator', outdir=target, timeout=1000) # simulation start sim(target, check_activity=False) return res
def decimate_filtfilt(x, q, n=None, ftype='iir', axis=-1, zerophase=True): """ Downsample the signal by using a filter. By default, an order 8 Chebyshev type I filter is used. A 30 point FIR filter with hamming window is used if `ftype` is 'fir'. Parameters ---------- x : ndarray The signal to be downsampled, as an N-dimensional array. q : int The downsampling factor. n : int, optional The order of the filter (1 less than the length for 'fir'). ftype : str {'iir', 'fir'}, optional The type of the lowpass filter. axis : int, optional The axis along which to decimate. Returns ------- y : ndarray The down-sampled signal. See also -------- resample """ if not isinstance(q, int): raise TypeError("q must be an integer") if n is None: if ftype == 'fir': n = 30 else: n = 8 if ftype == 'fir': b = firwin(n + 1, 1. / q, window='hamming') a = 1. else: b, a = cheby1(n, 0.05, 0.8 / q) if zerophase == True: y = signal.filtfilt(b, a, x, axis=axis) elif zerophase == False: y = signal.lfilter(b, a, x, axis=axis) sl = [slice(None)] * y.ndim sl[axis] = slice(None, None, q) return y[sl]
def decimate(x, q, n=None, ftype='iir', axis=-1): """Downsample the signal `x` by an integer factor `q`, using an order `n` filter. By default, an order 8 Chebyshev type I filter is used. A 30 point FIR filter with hamming window is used if `ftype` is 'fir'. Parameters ---------- x : N-d array the signal to be downsampled q : int the downsampling factor n : int or None the order of the filter (1 less than the length for 'fir') ftype : {'iir' or 'fir'} the type of the lowpass filter axis : int the axis along which to decimate Returns ------- y : N-d array the down-sampled signal See also -------- resample """ if not isinstance(q, int): raise TypeError("q must be an integer") if n is None: if ftype == 'fir': n = 30 else: n = 8 if ftype == 'fir': b = firwin(n + 1, 1. / q, window='hamming') a = 1. else: b, a = cheby1(n, 0.05, 0.8 / q) y = lfilter(b, a, x, axis=axis) sl = [slice(None)] * y.ndim sl[axis] = slice(None, None, q) return y[sl]
def decimate_filtfilt(x, q, n=None, ftype='iir', axis=-1): """ Downsample the signal by using a filter. By default, an order 8 Chebyshev type I filter is used. A 30 point FIR filter with hamming window is used if `ftype` is 'fir'. Parameters ---------- x : ndarray The signal to be downsampled, as an N-dimensional array. q : int The downsampling factor. n : int, optional The order of the filter (1 less than the length for 'fir'). ftype : str {'iir', 'fir'}, optional The type of the lowpass filter. axis : int, optional The axis along which to decimate. Returns ------- y : ndarray The down-sampled signal. See also -------- resample """ if not isinstance(q, int): raise TypeError("q must be an integer") if n is None: if ftype == 'fir': n = 30 else: n = 8 if ftype == 'fir': b = firwin(n + 1, 1. / q, window='hamming') a = 1. else: b, a = cheby1(n, 0.05, 0.8 / q) y = filtfilt(b, a, x, axis=axis) sl = [slice(None)] * y.ndim sl[axis] = slice(None, None, q) return y[sl]
print("R={} Fsr={} sps={} nframes={}".format(R, Fsr, sps, nframes)) ## filter design arguements Fpass = 1000. # passband edge Fstop = 2400. # stopband edge Wp = Fpass / (Fsr) # pass normalized frequency Ws = Fstop / (Fsr) # stop normalized frequency print("Filtering...") ## Create a filter taps = 8 #br = ffd.remez(taps, [0, Wp, Ws, .5], [1,0], maxiter=10000) #br = ffd.firwin2(taps, [0, Wp, Ws, 1], [0, 1, 1, 0]) br = ffd.firwin(taps, cutoff=[Wp, Ws], window='blackmanharris', pass_zero=False) # Once you have the coefficients from a filter design, (b for FIR b and a for IIR) you can use # a couple different functions to perform the filtering: lfilter, convolve, filtfilt. Typically # all these functions operate similar: y = filtfilt(b,a,x) # If you have a FIR filter simply set a=1, x is the input signal, b is the FIR coefficients. data = lfilter(br, 1, data) # IQ multiplication dec_0_i = [] dec_0_q = [] dec_1_i = [] dec_1_q = [] d_0_iq = [] d_1_iq = []