Python FFT Filters
Python FFT Filters
(https://plotly.com/)(/graphing-libraries/)
Python/v3
(/python/v3) > Signal Suggest an (https://github.com/plotly/graphing-library-
Analysis (/python/v3/#signal- edit to this docs/tree/master/_posts/python-v3/signal-
analysis) > FFT Filters page analysis/fft-filters/)
FFT Filters
in Python/v3
Learn how filter out the frequencies of a signal by using low-pass, high-pass and band-pass FFT
filtering.
New to Plotly?
Plotly's Python library is free and open source! Get started (https://plotly.com/python/getting-
started/) by downloading the client and reading the primer
(https://plotly.com/python/getting-started/).
Imports
The tutorial below imports NumPy (http://www.numpy.org/), Pandas
(https://plotly.com/pandas/intro-to-pandas-tutorial/), SciPy (https://www.scipy.org/) and Plotly
(https://plotly.com/python/getting-started/).
https://plotly.com/python/v3/fft-filters/ 1/12
11/14/21, 6:17 AM Python Fft Filters
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np
import pandas as pd
import scipy
Import Data
An FFT Filter is a process that involves mapping a time signal from time-space to frequency-
space in which frequency becomes an axis. By mapping to this space, we can get a better
picture for how much of which frequency is in the original time signal and we can ultimately
cut some of these frequencies out to remap back into time-space. Such filter types include
low-pass, where lower frequencies are allowed to pass and higher ones get cut off -, high-
pass, where higher frequencies pass, and band-pass, which selects only a narrow range or
"band" of frequencies to pass through.
https://plotly.com/python/v3/fft-filters/ 2/12
11/14/21, 6:17 AM Python Fft Filters
data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/wind_s
peed_laurel_nebraska.csv')
df = data[0:10]
table = ff.create_table(df)
py.iplot(table, filename='wind-data-sample')
https://plotly.com/python/v3/fft-filters/ 3/12
11/14/21, 6:17 AM Python Fft Filters
trace1 = go.Scatter(
mode='lines',
name='Wind Data'
layout = go.Layout(
showlegend=True
trace_data = [trace1]
py.iplot(fig, filename='wind-raw-data-plot')
14
12
10
0 50 100 150
EDIT CHART
https://plotly.com/python/v3/fft-filters/ 4/12
11/14/21, 6:17 AM Python Fft Filters
Low-Pass Filter
A Low-Pass Filter is used to remove the higher frequencies in a signal of data.
fc is the cutoff frequency as a fraction of the sampling rate, and b is the transition band also
as a function of the sampling rate. N must be an odd number in our calculation as well.
https://plotly.com/python/v3/fft-filters/ 5/12
11/14/21, 6:17 AM Python Fft Filters
fc = 0.1
b = 0.08
N = int(np.ceil((4 / b)))
if not N % 2: N += 1
n = np.arange(N)
trace1 = go.Scatter(
x=list(range(len(new_signal))),
y=new_signal,
mode='lines',
name='Low-Pass Filter',
marker=dict(
color='#C54C82'
layout = go.Layout(
title='Low-Pass Filter',
showlegend=True
trace_data = [trace1]
py.iplot(fig, filename='fft-low-pass-filter')
https://plotly.com/python/v3/fft-filters/ 6/12
11/14/21, 6:17 AM Python Fft Filters
Low-Pass Filter
EDIT CHART
High-Pass Filter
Similarly a High-Pass Filter will remove the lower frequencies from a signal of data.
Again, fc is the cutoff frequency as a fraction of the sampling rate, and b is the transition
band also as a function of the sampling rate. N must be an odd number.
Only by performing a spectral inversion afterwards after setting up our Low-Pass Filter will
we get the High-Pass Filter.
https://plotly.com/python/v3/fft-filters/ 7/12
11/14/21, 6:17 AM Python Fft Filters
fc = 0.1
b = 0.08
N = int(np.ceil((4 / b)))
if not N % 2: N += 1
n = np.arange(N)
window = np.blackman(N)
# reverse function
sinc_func = -sinc_func
sinc_func[int((N - 1) / 2)] += 1
trace1 = go.Scatter(
x=list(range(len(new_signal))),
y=new_signal,
mode='lines',
name='High-Pass Filter',
marker=dict(
color='#424242'
layout = go.Layout(
title='High-Pass Filter',
showlegend=True
trace_data = [trace1]
py.iplot(fig, filename='fft-high-pass-filter')
https://plotly.com/python/v3/fft-filters/ 8/12
11/14/21, 6:17 AM Python Fft Filters
High-Pass Filter
−2
EDIT CHART
Band-Pass Filter
The Band-Pass Filter will allow you to reduce the frequencies outside of a defined range of
frequencies. We can think of it as low-passing and high-passing at the same time.
In the example below, fL and fH are the low and high cutoff frequencies respectively as a
fraction of the sampling rate.
https://plotly.com/python/v3/fft-filters/ 9/12
11/14/21, 6:17 AM Python Fft Filters
fL = 0.1
fH = 0.3
b = 0.08
N = int(np.ceil((4 / b)))
n = np.arange(N)
# low-pass filter
hlpf *= np.blackman(N)
# high-pass filter
hhpf *= np.blackman(N)
hhpf = -hhpf
hhpf[int((N - 1) / 2)] += 1
h = np.convolve(hlpf, hhpf)
new_signal = np.convolve(s, h)
trace1 = go.Scatter(
x=list(range(len(new_signal))),
y=new_signal,
mode='lines',
name='Band-Pass Filter',
marker=dict(
color='#BB47BE'
layout = go.Layout(
title='Band-Pass Filter',
showlegend=True
trace_data = [trace1]
py.iplot(fig, filename='fft-band-pass-filter')
https://plotly.com/python/v3/fft-filters/ 10/12
11/14/21, 6:17 AM Python Fft Filters
Band-Pass Filter
−1
−2
−3
0 50 100 150 200 250
EDIT CHART
(https://dash.plotly.com)
https://plotly.com/python/v3/fft-filters/ 11/12
11/14/21, 6:17 AM Python Fft Filters
Copyright © 2021 Plotly. All rights Terms of Service (https://plotly.com/terms-of- Privacy Policy
reserved. service/) (https://plotly.com/privacy/)
https://plotly.com/python/v3/fft-filters/ 12/12