How do I properly design a digital FIR filter? - matlab

I want to design a series of optimal filters for ultrasonic signals. The filter order is fixed at 16 and I want the filter to have a passband for a list of ranges, i.e. (16kHz-38kHz, 17kHz-39kHz etc.). I've been using Matlab's Filter Designer and trying to different design inputs in a trial and error manner. What method can I use to find the optimal filter design algorithm (Remez, least-windows or something else) rather than haphazardly throwing inputs to the wall and seeing what looks best (I'm looking for a filter that best attenuates in the stopbands and has a flat frequency response in the passband, given my filter order constraint).

16 taps is not much to work with for an FIR filter, so I'm a dubious whether satisfying results are possible with any design. That said, I suggest trying Remez, aka Parks–McClellan optimal FIR filter design. See Matlab's firpm function. FIR filters designed by the Remez algorithm are "optimal" in the sense of minimizing the maximum pointwise error. Anthor plus about Remez is it allows setting different importance on each band, which could be useful to balance the tradeoff between passband flatness vs. stopband attenuation.

Related

How can the ideal low pass filter from the frequency domain can be applied?

I have an image where I add a Gaussian noise. I need to use the ideal low pass filter to remove the noise but I cannot really see any examples on the official Matlab documentation. There examples but not with images and I cannot really grasp the concept behind this filter. So could somebody explain how the ideal low pass filter can be used to remove noise?
image = imread('eight.tif');
imshow(image );
noisyImage = imnoise(image,'gaussian',0.02);
imshow(noisyImage);
If you know the standard deviation of the noise, It's good to use a Gaussian filter with that specific standard deviation. Although in most of the cases, it's good to use Bilateral filter (imbilatfilt) which is a gaussian filter with some other features that preserves the edges.
If you don't know what your noise is, It's best to use Wiener filter ([J,noise_out] = wiener2(I,[m n])). This filter observes the frequency behavior of the image and looks for a special pattern which is statistically consistent with noise. In other word, it estimate the noise of image and filter that specific noise for you. noise_out is the estimates of the additive noise power and m,n are the sizes of the filter's kernel (which I suggest something like 5*5 or 7*7).
Of course there are some other filtering methods including handmade ones, but those need more effort and lots of trial and error.

Designing an FIR filter with FDAtool using the order number and the cut-off frequency

I need to design an FIR high-pass filter to attenuate frequencies from 20Hz and below. I need the order to be around 8 since I will be implementing the filter on a microcontroller. When using MATLAB FDAtool, there are only Fpass and Fstop as the input parameters. Is there an option to input Fc only (since Fpass and Fstop are not known)? or is there a way to determine Fpass and Fstop from the order number and the cut-off frequency?
It may be a bit naive to think about implementing a filter and consider only the cut-off frequency. In reality, there are several parameters to a filter which must be considered. An ideal filter that only has cut-off frequency would be infinite order, and therefore impossible to implement.
Look at this image to get an idea of the different considerations present in real filter design.
But I'll go ahead and try to read the tea leaves, here:
Select highpass. You will probably want to choose 20Hz as the value for Fstop, though you should be aware that frequencies slightly above this value will also be slightly (but less-so) attenuated. Fpass is the frequency at which the attenuation effectively stops.
Your pass and stop bands will have an amplitude ripple that you will also probably want to specify. Tightening these will increase the order of your filter.

Real time digital filter with zero phase

In matlab we can use filtfilt function to filter out data which implements forward and backward filtering techniques which results in zero-phase. But it's difficult to implement this filter in real time as it involves backward filtering.
I want to implement a 1st order high-pass or low pass filter with zero phase in real time. How can i achieve this?
I have search the web for days but unable to get any clue to start with it!
Thanks in advance!
It is not possible to perform a zero-phase filter in real-time because a zero-phase filter requires filter coefficients that are symmetric around zero. That means that the filter is non-causal, or that current output depends on future input. This of course is not possible in the real-time case and can be faked as in the case of filtfilt during post-processing.
What you probably are looking for is a linear phase filter. Don't let the name confuse you; this does not mean that the filter produces any phase distortion. It only means that a time shift is applied to the output. A linear phase shift with respect to frequency results in a constant shift with respect to time. So basically your output will be delayed some constant number of samples (group delay) from the input.
So the only difference between a zero-phase and linear-phase filter is that the linear-phase filter output is a delayed version of the zero-phase output. This delay can be accounted for by keeping track of the group delay if you need to keep the output aligned in time with the input.
Response to comment:
FIR filters are guaranteed to be linear phase if their coefficients are symmetric about the center. MATLAB can easily create these types of filters with functions such as fir1 or firpm. The examples in the documentation of those functions should show you how to use them.
The group delay of a linear phase FIR filter is (L-1)/2 where L is the length of the filter. Because of this and a few other things, I would usually choose an odd filter length so the delay is aligned to a sample and not in between samples. This basically means that the output signal will be delayed from the input by (L-1)/2 samples.
Implementing the actual filtering process is basically discrete convolution of the input with the filter. This involves reversing the filter coefficients, multiplying them by the most recent L input samples, and adding those results to produce a single output sample. Then a new input sample is brought in and the whole process is done again to produce another sample (mutiplying and summing over a sliding window). You should be able to find some sample code for convolution on the web.
This is the direct way to perform FIR filtering, but for longer filters, it may be more efficient to perform fast convolution with an FFT. This will be a lot more difficult to get right, so unless you are talking about high sample rates and long filters, I would just go with the direct approach.
A "non-causal" zero-phase filter plus a sufficient amount of added delay can be approximated by a causal linear phase FIR filter. This assumes that adding some delay fits your system requirements.
In your case, you could take the impulse response of your forward+backward asymmetric (or non-linear phase) filtering process, window that impulse response to make the response finite in length, delay the finite length kernel so that it doesn't require "future" samples, and use that as a FIR filter kernel. You will have to check the results to see if your chosen window was appropriate in length and shape. There may be a trade-off in choosing a delay versus quality of the filter due to the finite length windowing required for that delay.

difference between the gaussian LPF and ideal LPF in frequency domain in image processing

I am working with the same image and I also need to remove the texture from the image posted in this link
How can I remove the texture from an image using matlab?
Discussions were made on this and I'm quite confused which filter(gaussian LPF or ideal lowpass) is really needed and what is the reason behind this.Which frequencies contribute for this texture????please can someone explain me!
An ideal low pass filter will keep all spatial frequencies below a nominal spatial frequency, and remove all spatial frequencies above it. Unfortunately, a true ideal low pass filter has infinite support (i.e., has an infinitely large non-zero spatial extend). Even a practical approximation to an ideal low pass filter has large spatial support.
A Gaussian, on the other hand, isn't ideal in terms of which frequencies it filters out. A Gaussian in the spatial domain turns out to be a Gaussian in the spatial frequency domain. That is, it doesn't produce very sharp spatial frequency selectivity. The advantage though is that the spatial support of the filter is small. People use Gaussian filters for this because they are convenient mostly. Filtering with a Gaussian tends to look "natural" compared to ideal low pass filters, which can generate ringing artifacts.
A Lanczos filter (windowed sinc filter) is also another choice as it will have a small spatial support and will approximate an ideal filter better than a Gaussian.
However, which is better for your image largely depends on what you want to do. While there's significant theory behind it, qualitative choices like this in image processing are largely an art.
The type of filter you are looking for is ideally nonlinear:
smoothing in areas without large-scale gradients (edges), and
little smoothing close to edges to be preserved.
Here are two alternatives:
The Kuwahara filter:
http://homepage.tudelft.nl/e3q6n/publications/1999/PAA99DRBDPVLV/PAA99DRBDPVLV.pdf
Enhanced shortening flow (Figure 8) in:
http://www.cs.jhu.edu/~misha/Fall07/Papers/intro-to-scalespace.pdf
In the second filter (Enhanced shortening flow), you can
vary the scale parameter and the nonlinear function,
h(Lw) on page 17. Thus, more trimming possibilities.
Ideally, the filter is completely isotropic
(same frequency effect on each possible angle).
Michael

High-pass filtering in MATLAB

Does anyone know how to use filters in MATLAB?
I am not an aficionado, so I'm not concerned with roll-off characteristics etc — I have a 1 dimensional signal vector x sampled at 100 kHz, and I want to perform a high pass filtering on it (say, rejecting anything below 10Hz) to remove the baseline drift.
There are Butterworth, Elliptical, and Chebychev filters described in the help, but no simple explanation as to how to implement.
There are several filters that can be used, and the actual choice of the filter will depend on what you're trying to achieve. Since you mentioned Butterworth, Chebyschev and Elliptical filters, I'm assuming you're looking for IIR filters in general.
Wikipedia is a good place to start reading up on the different filters and what they do. For example, Butterworth is maximally flat in the passband and the response rolls off in the stop band. In Chebyschev, you have a smooth response in either the passband (type 2) or the stop band (type 1) and larger, irregular ripples in the other and lastly, in Elliptical filters, there's ripples in both the bands. The following image is taken from wikipedia.
So in all three cases, you have to trade something for something else. In Butterworth, you get no ripples, but the frequency response roll off is slower. In the above figure, it takes from 0.4 to about 0.55 to get to half power. In Chebyschev, you get steeper roll off, but you have to allow for irregular and larger ripples in one of the bands, and in Elliptical, you get near-instant cut off, but you have ripples in both bands.
The choice of filter will depend entirely on your application. Are you trying to get a clean signal with little to no losses? Then you need something that gives you a smooth response in the passband (Butterworth/Cheby2). Are you trying to kill frequencies in the stopband, and you won't mind a minor loss in the response in the passband? Then you will need something that's smooth in the stop band (Cheby1). Do you need extremely sharp cut-off corners, i.e., anything a little beyond the passband is detrimental to your analysis? If so, you should use Elliptical filters.
The thing to remember about IIR filters is that they've got poles. Unlike FIR filters where you can increase the order of the filter with the only ramification being the filter delay, increasing the order of IIR filters will make the filter unstable. By unstable, I mean you will have poles that lie outside the unit circle. To see why this is so, you can read the wiki articles on IIR filters, especially the part on stability.
To further illustrate my point, consider the following band pass filter.
fpass=[0.05 0.2];%# passband
fstop=[0.045 0.205]; %# frequency where it rolls off to half power
Rpass=1;%# max permissible ripples in stopband (dB)
Astop=40;%# min 40dB attenuation
n=cheb2ord(fpass,fstop,Rpass,Astop);%# calculate minimum filter order to achieve these design requirements
[b,a]=cheby2(n,Astop,fstop);
Now if you look at the zero-pole diagram using zplane(b,a), you'll see that there are several poles (x) lying outside the unit circle, which makes this approach unstable.
and this is evident from the fact that the frequency response is all haywire. Use freqz(b,a) to get the following
To get a more stable filter with your exact design requirements, you'll need to use second order filters using the z-p-k method instead of b-a, in MATLAB. Here's how for the same filter as above:
[z,p,k]=cheby2(n,Astop,fstop);
[s,g]=zp2sos(z,p,k);%# create second order sections
Hd=dfilt.df2sos(s,g);%# create a dfilt object.
Now if you look at the characteristics of this filter, you'll see that all the poles lie inside the unit circle (hence stable) and matches the design requirements
The approach is similar for butter and ellip, with equivalent buttord and ellipord. The MATLAB documentation also has good examples on designing filters. You can build upon these examples and mine to design a filter according to what you want.
To use the filter on your data, you can either do filter(b,a,data) or filter(Hd,data) depending on what filter you eventually use. If you want zero phase distortion, use filtfilt. However, this does not accept dfilt objects. So to zero-phase filter with Hd, use the filtfilthd file available on the Mathworks file exchange site
EDIT
This is in response to #DarenW's comment. Smoothing and filtering are two different operations, and although they're similar in some regards (moving average is a low pass filter), you can't simply substitute one for the other unless it you can be sure that it won't be of concern in the specific application.
For example, implementing Daren's suggestion on a linear chirp signal from 0-25kHz, sampled at 100kHz, this the frequency spectrum after smoothing with a Gaussian filter
Sure, the drift close to 10Hz is almost nil. However, the operation has completely changed the nature of the frequency components in the original signal. This discrepancy comes about because they completely ignored the roll-off of the smoothing operation (see red line), and assumed that it would be flat zero. If that were true, then the subtraction would've worked. But alas, that is not the case, which is why an entire field on designing filters exists.
Create your filter - for example using [B,A] = butter(N,Wn,'high') where N is the order of the filter - if you are unsure what this is, just set it to 10. Wn is the cutoff frequency normalized between 0 and 1, with 1 corresponding to half the sample rate of the signal. If your sample rate is fs, and you want a cutoff frequency of 10 Hz, you need to set Wn = (10/(fs/2)).
You can then apply the filter by using Y = filter(B,A,X) where X is your signal. You can also look into the filtfilt function.
A cheapo way to do this kind of filtering that doesn't involve straining brain cells on design, zeros and poles and ripple and all that, is:
* Make a copy of the signal
* Smooth it. For a 100KHz signal and wanting to eliminate about 10Hz on down, you'll need to smooth over about 10,000 points. Use a Gaussian smoother, or a box smoother maybe 1/2 that width twice, or whatever is handy. (A simple box smoother of total width 10,000 used once may produce unwanted edge effects)
* Subtract the smoothed version from the original. Baseline drift will be gone.
If the original signal is spikey, you may want to use a short median filter before the big smoother.
This generalizes easily to 2D images, 3D volume data, whatever.