Hello fellow oceanographers, I just trying to design a fourth-order low-pass butterworth filter with a 15-days cutoff period, in python, with the aim to filter a ocean bottom pressure (OBP) time series sampled every hour. This is my code:

import pandas as pd
import numpy as np
import scipy 
from scipy.signal import butter, filtfilt, sosfiltfilt

M = len(OBP['SPFO4']) # Number of points 
# OBP['SPFO4'] is the OBP time series.

# === Applying the tapering function
beta = 2
window = scipy.signal.windows.kaiser(M,beta,sym=True)
OBP['SPFO4'] = window * OBP['SPFO4']

########### Applying the filter

fs = 1 # Sampling frecuency (sampled every hour)
fc = 1/360 # 24(hours)*15(days) = 360 hours, 
 # Am I correct in this step ?
fc_norm = fc/(fs/2) 
# Define the filter order (4th order is commonly used)
filter_order = 2 # I used 2 because it is a forward-backward filtering,
                 # so the input is mulplied by two ?

# Generate the Butterworth filter coefficients
b, a = butter(filter_order, fc_norm, btype='lowpass',analog=False)

OBP['SPFO4']= filtfilt(b, a,OBP['SPFO4'])

I have documented myself to build a code correctly in the sense of signal analysis, since I am new to this. So my concern is that (1) if my code is well written for the purposes I need it to be? (2) As you know, I am using signal.filtfilt(), but I have seen other option: signal.sosfiltfilt(). I am confused because the two pictures obtained look a little different. Why?

Anyone could help me to understand the previous questions and and what depends on using a certain order in a butterworth filter (I took the order from this article: https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2020JB020065, page 4, second paragraph)? Any scientific article that explain this would be grateful. I attached the pictures.

This is obtained using signal.filtfilt()

enter image description here

This is obtained using signal.sosfiltfilt()

enter image description here


1 Answer 1


I think your best bet is to use the filter that comes with scipy: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html

from scipy import signal

import numpy as np

b, a = signal.butter(4, 100, 'low', analog=True)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.