1

I want to smooth a medical image using a butterworth filter, the data is very noisy and I want to reduce this. I am using Python v3.7. The image data is stored in a 2D np.array, which I transformed to the frequency domain using scipy. I don't know what step is next to be able to apply a butterworth filter

#%% butterworth filter
import scipy.fftpack
import scipy.signal
normal_scan=scan_spect # I have already loaded and preprocessed the data 
freq_scan=scipy.fftpack.fft2(normal_scan)

N=10 #order/power of the filter

Wn=0.6 #critical frequency

B, A=scipy.signal.butter(10,0.6, output='ba' )

smoothed_data=scipy.signal.filtfilt(B, A, freq_scan)

What format does my data have to be in to be able to apply the butterworth filter? And which parameters do I use.

1 Answer 1

4

This is my approach I don't know if it clears enough noise for you . I am also suggesting to make the order 4

def butterLow(cutoff, critical, order):
    normal_cutoff = float(cutoff) / critical
    b, a = signal.butter(order, normal_cutoff, btype='lowpass')
    return b, a

def butterFilter(data, cutoff_freq, nyq_freq, order):
    b, a = butterLow(cutoff_freq, nyq_freq, order)
    y = signal.filtfilt(b, a, data)
    return y

x=np.array(freq_scan)
cutoff_frequency = some value
sample_rate = maximum value in your array *2 +1

y = butterFilter(x, cutoff_frequency, sample_rate/2)

This will give you the low pass butterworth if you want the high pass output:

high=np.array(x)-np.array(y)

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.