A
A
Antongard2019-06-01 08:39:12
Python
Antongard, 2019-06-01 08:39:12

How to make a windowed bandpass filter?

Hello.
There is some complex signal. It has a lot of frequencies (from 0 to 100, for example), this can be obtained using the Fourier transform. But it is required to leave only certain frequencies (for example, from 10 to 50) and output the signal back only with these certain frequencies.
I tried to make a Fourier transform, zeroing out unnecessary frequencies, an inverse Fourier transform, but something went wrong.
I recently learned about the window band pass filter. Don't know how to make it in Python?
Thank you)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
longclaps, 2019-06-01
@longclaps

import numpy as np, matplotlib.pyplot as plt
from scipy.fftpack import rfft, irfft

n = 2 ** 8
x = np.linspace(0, 2 * np.pi, n)
y = np.sin(x) + np.cos(x * 30)
f0 = rfft(y)
f1 = f0.copy()
f1[:n // 8] = 0
z = irfft(f1)
f, (spectra, signal) = plt.subplots(2, 1, sharey=False)
spectra.plot(x, f0, label='f0')
spectra.plot(x, f1, label='f1')
spectra.legend()
spectra.title.set_text('spectra')
signal.plot(x, y, label='y')
signal.plot(x, z, label='z')
signal.legend()
signal.title.set_text('signal')
plt.legend()
plt.show()

P
Papayaved, 2019-06-06
@Papayaved

Window functions are needed to reduce the sidelobes in the Fourier transform. Sidelobes are the appearance of false harmonics in a signal due to a limited number of samples. The ideal Fourier transform does over infinite limits with an infinitesimal step, and the DFT introduces its own specific errors.
Zeroing unnecessary frequencies in the spectrum corresponds to multiplying by a rectangular window function. In order to avoid false harmonics in the signal, it is necessary to multiply the spectrum by the window function of a more complex form (see Wikipedia), the increase in the number of samples also reduces the side lobes, i.e. increasing the number of points by adding zeros.
There is another concept, the windowed Fourier transform, this is the Fourier transform by a sliding window. Moreover, the use of the FFT is no longer rational, they consider the usual DFT, recalculating the spectrum upon receipt of each new sample.

P
polybook, 2019-06-06
@polybook

It is not necessary to do it through the Fourier transform, you can remove the trend (LF) + make a moving average (HF smoothing). The parameters of the low-pass and high-pass filters can be controlled by doing a Fourier transform before and after. Read more: comma.polybook.ru/1.7.3.pdf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question