<p>I think your best bet is to use the filter that comes with scipy: <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html" rel="nofollow noreferrer">https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html</a></p>
<blockquote>
<p>from scipy import signal</p>
</blockquote>
<blockquote>
<p>import numpy as np</p>
</blockquote>
<blockquote>
<p>b, a = signal.butter(4, 100, 'low', analog=True)</p>
</blockquote>

CC BY-SA 4.0
2023-08-28T21:20:41.003Z
2023-08-28T21:20:41.003Z
arkaia
<p>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:</p>
<pre><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'])
</code></pre>
<p>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?</p>
<p>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: <a href="https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2020JB020065" rel="nofollow noreferrer">https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2020JB020065</a>, page 4, second paragraph)?
Any scientific article that explain this would be grateful. I attached the pictures.</p>
<p>This is obtained using signal.filtfilt()</p>
<p><a href="https://i.sstatic.net/WOvXf.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/WOvXf.png" alt="enter image description here" /></a></p>
<p>This is obtained using signal.sosfiltfilt()</p>
<p><a href="https://i.sstatic.net/Og07D.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Og07D.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2023-08-28T17:47:33.270Z
What article are you referring to?
arkaia
CC BY-SA 4.0
2023-08-28T18:02:56.593Z
Sorry, this is the article: https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2020JB020065
Miguel 
<oceanography><python>
A low-pass butterworth filter to analyze ocean bottom pressure
Miguel 
# A low-pass butterworth filter to analyze ocean bottom pressure

(Asked by: username_0 on 2023-08-27)

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](https://i.sstatic.net/WOvXf.png)](https://i.sstatic.net/WOvXf.png)

This is obtained using signal.sosfiltfilt()

[![enter image description here](https://i.sstatic.net/Og07D.png)](https://i.sstatic.net/Og07D.png)

**Tags:** oceanography, python

> **Comments:**
> **username_1**: What article are you referring to?  
> **username_0**: Sorry, this is the article: https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2020JB020065  

---

## 1 answers

### Answer by username_1 on 2023-08-28. Score: 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)


---

