LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
fir_design.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""!
4Author: J.A. de Jong - ASCEE
5
6Description: Designs octave band FIR filters from 16Hz to 16 kHz for a sampling
7frequency of 48 kHz.
8"""
9# from asceefigs.plot import Bode, close, Figure
10__all__ = ['freqResponse', 'bandpass_fir_design', 'lowpass_fir_design',
11 'arbitrary_fir_design']
12
13import numpy as np
14from scipy.signal import freqz, hann, firwin2
15
16
17def freqResponse(fs, freq, coefs_b, coefs_a=1.):
18 """
19 Computes the frequency response of the filter defined with the filter
20 coefficients:
21
22 Args:
23 fs: Sampling frequency [Hz]
24 freq: Array of frequencies to compute the response for
25 coefs_b: Forward coefficients (FIR coefficients)
26 coefs_a: Feedback coefficients (IIR)
27
28 Returns:
29 Complex frequency response for frequencies given in array
30 """
31 Omg = 2*np.pi*freq/fs
32
33 w, H = freqz(coefs_b, coefs_a, worN=Omg)
34 return H
35
36
37def bandpass_fir_design(L, fs, fl, fu, window=hann):
38 """
39 Construct a bandpass filter
40 """
41 assert fs/2 > fu, "Nyquist frequency needs to be higher than upper cut-off"
42 assert fu > fl, "Cut-off needs to be lower than Nyquist freq"
43
44 Omg2 = 2*np.pi*fu/fs
45 Omg1 = 2*np.pi*fl/fs
46
47 fir = np.empty(L, dtype=float)
48
49 # First Create ideal band-pass filter
50 fir[L//2] = (Omg2-Omg1)/np.pi
51
52 for n in range(1, L//2):
53 fir[n+L//2] = (np.sin(n*Omg2)-np.sin(n*Omg1))/(n*np.pi)
54 fir[L//2-n] = (np.sin(n*Omg2)-np.sin(n*Omg1))/(n*np.pi)
55
56 win = window(L, True)
57 fir_win = fir*win
58
59 return fir_win
60
61
62def lowpass_fir_design(L, fs, fc, window=hann):
63 assert fs/2 > fc, "Nyquist frequency needs to be higher" \
64 " than upper cut-off"
65
66 Omgc = 2*np.pi*fc/fs
67 fir = np.empty(L, dtype=float)
68
69 # First Create ideal band-pass filter
70 fir[L//2] = Omgc/np.pi
71
72 for n in range(1, L//2):
73 fir[n+L//2] = np.sin(n*Omgc)/(n*np.pi)
74 fir[L//2-n] = np.sin(n*Omgc)/(n*np.pi)
75
76 win = window(L, True)
77 fir_win = fir*win
78
79 return fir_win
80
81
82def arbitrary_fir_design(fs, L, freq, amps, window='hann'):
83 """
84 Last frequency of freq should be fs/2
85 """
86 return firwin2(L, freq, amps, fs=fs, window=window)