I am trying to filter out the low frequencies of an array looking like this :s

For now, I am taking transects of this array and applying a 1D - butterworth band pass filter :

import numpy as np
from scipy.signal import find_peaks, butter, sosfiltfilt, sosfreqz

def Transect_angle(A, angle, Sp, length):
    x0, y0 = Sp[0], Sp[1]
    x1, y1  = x0 + cosd(angle)*length, y0 + sind(angle)*length
    return Array_transect(A, [y0,x0], [y1,x1], 'nearest'), [x0,y0], [x1,y1]

def butter_bandpass(lowcut, highcut, fs, order=5):
        nyq = 0.5 * fs
        low = lowcut / nyq
        high = highcut / nyq
        sos = butter(order, [low, high], analog=False, btype='band', output='sos')
        return sos

def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
        sos = butter_bandpass(lowcut, highcut, fs, order=order)
        y = sosfiltfilt(sos, data)
        return y

transect, p0, p1 = Transect_angle(Array, angle, start ,length)
tr_withoutnan = transect[~np.isnan(transect)] ### removing nan
filtered = butter_bandpass_filter(tr_withoutnan, low_freq, High_freq, 1)

I would like to apply the same kind of filtering but directly in 2D to my array. I have two main problems :

  1. For me, filtering is basically a convolution between data and a filter. Based on this, I can directly make the convolution of my array with a butterworth filter having an axial symmetry. However, in my function butter_bandpass_filter, I am using sosfiltfilt, which mention that the filter is appplied forward and backward. I don't know what that means it terms of convolution, but I remember that someaone mentioned it was important in a topic I read a long time ago.

  2. Ho should I deal with the Nans values (in white in my image) ?

Finally, I use a butterworth bandpass filter because I read that it was the filter that was the least likely to produce artefacts. I am open to any suggestion !


1 Answer 1

  1. filtfilt() is a technique to achieve zero-phase filtering by applying the same filter twice to the data; with the output of the first stage reversed and filtered again in the second stage. Zero phase filtering is a desired property in image processing.

  2. NaN means "not a number" and indicates those indeterminate conditions like $0/0$, $\infty/\infty$, $\infty - \infty$. NaNs cannot be processed arithmetically, hence you should remove all the NaN samples with some suitable values before further processing.

  • $\begingroup$ Thank you. When going from 1D to 2D, how do you define the reversed array ? I feel like now, instead of applying the filter twice you have to apply it four times ? By reverting each axis independently, and then both of them ? $\endgroup$
    – Liris
    Nov 25, 2019 at 16:31
  • $\begingroup$ in 2D, imho the reversed signal array should be $x[-n,-m]$, at a single step. $\endgroup$
    – Fat32
    Nov 25, 2019 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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