0% found this document useful (0 votes)
37 views12 pages

Python FFT Filters

Uploaded by

solomon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

Python FFT Filters

Uploaded by

solomon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

11/14/21, 6:17 AM Python Fft Filters

([Link]

Python/v3
(/python/v3) > Signal Suggest an ([Link]
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.

Note: this page is part of the documentation for version 3 of


[Link], which is not the most recent version.
See our Version 4 Migration Guide ([Link]
migration/) for information about how to upgrade.

New to Plotly?
Plotly's Python library is free and open source! Get started ([Link]
started/) by downloading the client and reading the primer
([Link]

You can set up Plotly to work in online ([Link]


started/#initialization-for-online-plotting) or offline ([Link]
started/#initialization-for-offline-plotting) mode, or in jupyter notebooks
([Link]

We also have a quick-reference cheatsheet ([Link]


documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!

Imports
The tutorial below imports NumPy ([Link] Pandas
([Link] SciPy ([Link] and Plotly
([Link]

[Link] 1/12
11/14/21, 6:17 AM Python Fft Filters

import [Link] as py

import plotly.graph_objs as go

import plotly.figure_factory as ff

import numpy as np

import pandas as pd

import scipy

from scipy import signal

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.

Let us import some stock data to apply FFT Filtering:

[Link] 2/12
11/14/21, 6:17 AM Python Fft Filters

data = pd.read_csv('[Link]
peed_laurel_nebraska.csv')

df = data[0:10]

table = ff.create_table(df)

[Link](table, filename='wind-data-sample')

10 Min Std Dev Time 10 M

2.73 2001-06-11 11:00 22.3

1.98 2001-06-11 11:10 23.0

1.87 2001-06-11 11:20 23.3

2.03 2001-06-11 11:30 22.0

3.1 2001-06-11 11:40 20.5

2.3 2001-06-11 11:50 25.2

2.46 2001-06-11 12:00 24.8

1.87 2001-06-11 12:10 24.0

1.71 2001-06-11 12:20 22.9

1.76 2001-06-11 12:30 EDIT CHART


17.9

Plot the Data


Let's look at our data in its raw form before doing any filtering.

[Link] 3/12
11/14/21, 6:17 AM Python Fft Filters

trace1 = [Link](

x=list(range(len(list(data['10 Min Std Dev'])))),

y=list(data['10 Min Std Dev']),

mode='lines',

name='Wind Data'

layout = [Link](

showlegend=True

trace_data = [trace1]

fig = [Link](data=trace_data, layout=layout)

[Link](fig, filename='wind-raw-data-plot')

14

12

10

0 50 100 150

EDIT CHART

[Link] 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.

[Link] 5/12
11/14/21, 6:17 AM Python Fft Filters

fc = 0.1

b = 0.08

N = int([Link]((4 / b)))

if not N % 2: N += 1

n = [Link](N)

sinc_func = [Link](2 * fc * (n - (N - 1) / 2.))

window = 0.42 - 0.5 * [Link](2 * [Link] * n / (N - 1)) + 0.08 * [Link](4 * [Link] * n


/ (N - 1))

sinc_func = sinc_func * window

sinc_func = sinc_func / [Link](sinc_func)

s = list(data['10 Min Std Dev'])

new_signal = [Link](s, sinc_func)

trace1 = [Link](

x=list(range(len(new_signal))),

y=new_signal,

mode='lines',

name='Low-Pass Filter',

marker=dict(

color='#C54C82'

layout = [Link](

title='Low-Pass Filter',

showlegend=True

trace_data = [trace1]

fig = [Link](data=trace_data, layout=layout)

[Link](fig, filename='fft-low-pass-filter')

[Link] 6/12
11/14/21, 6:17 AM Python Fft Filters

Low-Pass Filter

0 50 100 150 200

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.

[Link] 7/12
11/14/21, 6:17 AM Python Fft Filters

fc = 0.1

b = 0.08

N = int([Link]((4 / b)))

if not N % 2: N += 1

n = [Link](N)

sinc_func = [Link](2 * fc * (n - (N - 1) / 2.))

window = [Link](N)

sinc_func = sinc_func * window

sinc_func = sinc_func / [Link](sinc_func)

# reverse function

sinc_func = -sinc_func

sinc_func[int((N - 1) / 2)] += 1

s = list(data['10 Min Std Dev'])

new_signal = [Link](s, sinc_func)

trace1 = [Link](

x=list(range(len(new_signal))),

y=new_signal,

mode='lines',

name='High-Pass Filter',

marker=dict(

color='#424242'

layout = [Link](

title='High-Pass Filter',

showlegend=True

trace_data = [trace1]

fig = [Link](data=trace_data, layout=layout)

[Link](fig, filename='fft-high-pass-filter')

[Link] 8/12
11/14/21, 6:17 AM Python Fft Filters

High-Pass Filter

−2

0 50 100 150 200

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.

[Link] 9/12
11/14/21, 6:17 AM Python Fft Filters

fL = 0.1

fH = 0.3

b = 0.08

N = int([Link]((4 / b)))

if not N % 2: N += 1 # Make sure that N is odd.

n = [Link](N)

# low-pass filter

hlpf = [Link](2 * fH * (n - (N - 1) / 2.))

hlpf *= [Link](N)

hlpf = hlpf / [Link](hlpf)

# high-pass filter

hhpf = [Link](2 * fL * (n - (N - 1) / 2.))

hhpf *= [Link](N)

hhpf = hhpf / [Link](hhpf)

hhpf = -hhpf

hhpf[int((N - 1) / 2)] += 1

h = [Link](hlpf, hhpf)

s = list(data['10 Min Std Dev'])

new_signal = [Link](s, h)

trace1 = [Link](

x=list(range(len(new_signal))),

y=new_signal,

mode='lines',

name='Band-Pass Filter',

marker=dict(

color='#BB47BE'

layout = [Link](

title='Band-Pass Filter',

showlegend=True

trace_data = [trace1]

fig = [Link](data=trace_data, layout=layout)

[Link](fig, filename='fft-band-pass-filter')

[Link] 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

([Link]

Products Pricing About Us

Dash Enterprise Pricing Careers


([Link] ([Link] ([Link]
Consulting and Training pricing/) Resources
([Link] ([Link]
and-oem/) Blog
([Link]

Support JOIN OUR MAILING LIST

[Link] 11/12
11/14/21, 6:17 AM Python Fft Filters

Community Support Sign up to stay in the loop with all


([Link] things Plotly — from Dash Club to
Documentation product
updates, webinars, and more!
([Link] S U B S C RI B E
libraries) ( H TTPS :/ / G [Link] / S U B S C RI PTI ON)

Copyright © 2021 Plotly. All rights Terms of Service ([Link] Privacy Policy
reserved. service/) ([Link]

[Link] 12/12

You might also like