def design(self, fs, fc, n_cycles=7.0, bandwidth=None, zero_mean=True):
"""
Designs a FIR filter that is a band-pass filter centered
on frequency fc.
fs : sampling frequency (Hz)
fc : frequency of the carrier (Hz)
n_cycles : number of oscillation in the wavelet
bandwidth : bandwidth of the FIR wavelet filter
(used when: bandwidth is not None and n_cycles is None)
zero_mean : if True, we make sure fir.sum() = 0
"""
# the length of the filter
if bandwidth is None and n_cycles is not None:
half_order = int(float(n_cycles) / fc * fs / 2)
order = 2 * half_order + 1
elif bandwidth is not None and n_cycles is None:
half_order = int(1.65 * fs / bandwidth) // 2
order = half_order * 2 + 1
else:
raise ValueError('Carrier.design: n_cycles and order cannot be '
'both None or both not None.')
w = np.blackman(order)
t = np.linspace(-half_order, half_order, order)
phase = (2.0 * np.pi * fc / fs) * t
car = np.cos(phase)
fir = w * car
# the filter must be symmetric, in order to be zero-phase
assert np.all(np.abs(fir - fir[::-1]) < 1e-15)
# remove the constant component by forcing fir.sum() = 0
if zero_mean:
fir -= fir.sum() / order
gain = np.sum(fir * car)
self.fir = fir * (1.0 / gain)
self.fs = fs
# add the imaginary part to have a complex wavelet
if self.extract_complex:
car_imag = np.sin(phase)
fir_imag = w * car_imag
self.fir_imag = fir_imag * (1.0 / gain)
return self
评论列表
文章目录