FIR / IIR and Wavelets - filtering

I am a self-learner of DSP by reading various books. I have accomplished basic understanding of Signals - CT and DT and a few transforms. I recently started to learn FIR / IIR Filters.
The part that I cannot understand is that they are termed as 'Filters' which for me logically means blocking / allowing from a threshold value - ex. lower values would be passed higher would be filtered or removed. So if we have a low pass filter - it would lead us to remove high values from a sequence and vice versa for high pass filters - is my understanding correct?
Like HPF (High Pass Filter):
x(n)={1,2,3,4,5,6,7,8,9}
Set threshold as >=5 so output sequence would be {5,6,7,8,9}
OK but the document states FIR / IIR about :
Finite Impulse Response (FIR) : this type of filter gives a finite
number of nonzero outputs (response) to an impulse function input. It
does not use feed-back.
While
Infinite Impulse Response (IIR) : this type of filter uses feed-back,
so it could have an infinite number of nonzero outputs (response) to
an impulse function input.
Now I cannot understand what FIR / IIR has to do with my concept of filters - allow / block high / low values. Where does the question of Feedback comes here?
Similarly for wavelets -
We call an octave a level of resolution, where each octave can be
envisioned as a pair of FIR filters, at least for the one-dimensional
case. One filter of the analysis (wavelet transform) pair is a lowpass
filter (LPF), while the other is a highpass filter (HPF). Each filter
has a down-sampler after it, to make the transform efficient. For
example, a simple lowpass filter may have coefficients {1/2,1/2},
producing outputs (x[n] + x[n - 1])/2, which is clearly the average of
two samples. A corresponding simple highpass filter would have
coefficients {1/2,-1/2}, producing outputs (x[n] - x[n - 1])/2, half
the difference of the samples.
I am not able to get the concept of how and why here equation : (x[n] + x[n - 1])/2 and (x[n] - x[n - 1])/2 is being referred?

High pass filter and Low pass filter in signal processing refer to a system which respectively passes high-frequency and low-frequency contents (equivalently respectively blocks low-frequency and high-frequency contents) from the signal, not low/high values.
So for example, given a sequence which is the sum of a constant value (low-frequency component) and a more rapidly varying alternating values (high-frequency component):
x(n) = {0, 2, 0, 2, 0, 2, 0, ...}
The output of a low-pass filter computing y[n] = (x[n] + x[n-1])/2 would look like:
y(n) = {1, 1, 1, 1, 1, 1, 1, ...}
Keeping only the low-frequency constant value.
Similarly, the output of a high-pass filter computing y[n] = (x[n] - x[n-1])/2 would look like:
y(n) = {1, -1, 1, -1, 1, -1, ...}
This time keeping only the high-frequency content of the signal (the alternating values).
Now, this 'selection' of low-frequency and high-frequency component can often be achieved as the solution to difference equations (a mathematical relation which combines various input and output samples). FIR and IIR are specific categorisations of those filters, which are distinguished by the dependency on previous output samples (eg. y[n-1]) in the difference equation (hence the notion of feedback). For example, a simple IIR low-pass filter (with different characteristics as the previous FIR low-pass filter example) could be implemented as the difference equation:
y[n] = 0.9*y[n-1] + 0.1*x[n];
Where the current output sample y[n] is expressed in terms of the previous output y[n-1].

Related

How to correctly bandpass filter in frequency with MATLAB?

i'm pretty new to matlab and signal processing, but I am working on a project and I am stuck. My goal is to do a band-pass fast Fourier transform (FFT) filter with cut-off frequencies of 0.5 Hz and 3 Hz from a real signal. My input signal is a signal sampled ad 405Hz and of around 35000 samples. My implementation looks like this:
%ZFiltered = My signal
filtered = bandpass(fft(zFiltered), [0.5 3], sampling_frequency);
res = ifft(filtered);
subplot(2,1,1)
plot(zFiltered)
subplot(2,1,2)
plot(abs(res))
My questions are: Why do I get a signal which contains also imaginary part? Why does it look like this ?
Your goal is unclear to me; here are three different approaches that your question hints at
filter your raw signal with a band-pass filter as your code attempts to do
use the fft and ifft functions to filter your raw signal
calculate the coefficients of a digital filter that would act as a band-pass filter when used with the filter function
Approach 1: filtering with bandpass function
Referring to the bandpass documentation, you would use your original signal and specify the filter band and sampling frequency as in:
filtered_signal = bandpass(zFiltered,[freq_low freq_high],sample_freq);
plot(zFiltered); hold on;
plot(filtered_signal);
where the inputs are the
zFiltered : original (unfiltered signal)
freq_low=0.5 : lowest frequency in Hz of the band pass range`
freq_high=3 : highest frequency in Hz of the band pass range
sample_freq=405 : the sampling frequency in Hz
Your approach using digital fast Fourier transform function fft produces complex results because you are looking at the actual components computed by the transform (which by definition are complex having both real and imaginary parts). You can compute the power of the signal with:
P = abs(res/n).^2;
which gives you the power of the signal for each frequency represented in the Fourier transform (see the fft function docs here for more information).
Approach 2: Filtering with fft ifft functions
Use the Fourier transform and inverse Fourier transform functions to filter the signal. The steps here are to use fft to get the signal into the frequency domain. Set the elements you want filtered to zero and then apply the inverse Fourier transform (ifft) to get the filtered signal. Each component represents a given discrete frequencies in the range between 0Hz and F_s/2 that contributes to the signal. Set components of the FFT you want to suppress to zoro and apply ifft to get back a filter signal. Please see the fft docs and ifft docs for information on these functions and some of the intricacies of working with signals in the frequency domain.
Approach 3: Filtering with the coefficeints of the standard difference equation
Computing the coefficients of a digital band pass filter. This approach calculates the coefficients (vectors A and B) of the standard difference equation (see filter docs for details). An example of a function that does this is butter which gives the coefficients that represent a Butterworth filter of a given frequency. You could also look at the designfilt function for more options.

How do I get the spectrum of 1D and 2D spatial filters used in images?

I would like to know how to obtain the spectrum of a spatial 1 dimensional and 2 dimensional sharpening filter in image processing.
The sharpening filters are:
[-1, 3, -1];
[-1, -1, -1; -1, 8, -1; -1, -1, -1];
In MATLAB, what should I do to get the spectrum of the filters, or how do I get the frequency components of these filters?
You can use Luis Mendo's post to determine the spectrum of your filter right away. Use either freqz for a 1D filter, or freqz2 for a 2D filter. Note that the plot using freqz and freqz2 are in terms of normalized frequency that spans between [-1,1].
However, I'd like to write this post to compliment Luis Mendo's and show you how the spectrum is derived.
Let's start with your first filter:
h1 = [-1,3,-1];
If you recall, the 1D Discrete-Time Fourier Transform (DTFT) is defined as:
Aside: Why the DTFT and not the Discrete Fourier Transform (DFT)?
Take special care that this is different from the Discrete Fourier Transform (DFT). The difference between them is that the DTFT is the traditional Fourier Transform applied to a discrete signal. From the continuous viewpoint, we apply the normal Fourier Transform where the spectrum is continuous in terms of frequency. It is essentially the same thing here for a discrete signal, but the transform is applied to a discrete signal. The output is also continuous in frequency, with the additional constraint that the spectrum is 2*pi periodic. For the DFT, it is essentially a sampled version of the DTFT in frequency domain. Therefore, the core difference between them both is that in the frequency domain, one is continuous while the other is a sampled version of the continuous counterpart. The reason why we need to sample the DTFT is so that you can actually store the spectra on computers and let programs process the spectra (i.e. MATLAB).
Your question asked to determine what the spectrum is, and in terms of a theoretical perspective, I will present the DTFT to you. When we actually plot the spectra, we are actually sampling the DTFT anyway, so when we see the spectra, you would be visualizing the DFT (more or less!).
A very good explanation about the differences between them both can be found on DSP StackExchange: https://dsp.stackexchange.com/questions/16586/difference-between-discrete-time-fourier-transform-and-discrete-fourier-transfor
Back to your question
For the definition of the DTFT, h[k] is a 1D signal, and omega is the angular frequency defined in radians. Therefore, you can consider your filter to be this 1D signal and when you filter in the spatial domain, it is the same as taking this signal, transforming it into the frequency domain and performing multiplication with another input signal in the frequency domain.
Therefore, if we consider that your filter is symmetric, the value of 3 is defined as the centre point. As such, you can think of h[k] as:
h = [-1 3 -1]
^ ^ ^
k = -1 0 1
Therefore, the frequency domain representation is simply a weighted sum of the filter coefficients with complex exponential terms. Substituting h[k] into the Fourier Transform formula, we get:
If you recall Euler's formula, we have:
Similarly:
If we add the two equations together, we can rearrange and solve for cos(x):
Going back to our transform of our 1D filter, we can do some factoring:
Note that the domain is between [-pi,pi], as the 1D Fourier Transform is 2*pi symmetric. As such, if you want to show the spectrum, simply plot using the above domain and use the equation that I just created, plotting the absolute value, as the magnitude of the spectrum is always positive:
w = linspace(-pi,pi);
h = abs(3 - 2*cos(w));
plot(w,h);
title('Magnitude of 1D spectrum');
axis([-pi, pi, 0, 5]);
linspace generates a linearly increasing array from -pi to pi with 100 points in between the ranges. You can override this behaviour by specifying a third parameter that manually tells linspace how many points you want to generate, but the default is 100 if you omit this parameter.
Note that I've made the y-axis extend from 0 so you can see where the curve starts at and it going up to 5 as this is the maximum value that is ever possible for h. This is what we get:
Indeed the above spectrum certainly looks like a high-pass filter, and because the magnitude at the DC frequency (w = 0) is 1, you are essentially adding the original signal on top of the high-pass filtering results, which thus "sharpens" your signal.
You can do the same process with the 2D case, though it will be a bit more involved. In the 2D case, the Discrete-Time Fourier Transform is defined to be:
We have two independent variables to consider, and we will have 2D spatial frequencies. w1 and k1 will operate along the rows of the 2D signal and w2 and k2 will operate along the columns of the 2D signal.
Given your mask:
h2 = [-1 -1 -1; -1 8 -1; -1 -1 -1]
As the shape of h2 is symmetric, the value of 8 will be location (w1,w2) = (0,0). Therefore, when we calculate the spectrum with the above equation, we get:
I'll save you the trouble with the simplification, but doing some rearranging and making use of Euler's formula, we get:
Note that:
I used the above property to make the simplifications possible. For the 2D case, we will define a meshgrid of coordinates that both range between [-pi,pi] for both dimensions, and we will plot this on a surface plot with surf. Remember to take the absolute value of the filter to show the magnitude:
[w1,w2] = meshgrid(linspace(-pi,pi));
h = abs(8 - 2*cos(w1+w2) - 2*cos(w1) - 2*cos(w2) - 2*cos(w1-w2));
surf(w1,w2,h);
title('2D spectrum of filter');
w1 and w2 provide unique combinations of the frequencies defined horizontally and vertically at each respective spatial location of the arrays. w1 and w2 will in fact be two-dimensional. For each unique combination of w1 and w2, we determine what the magnitude of the spectra will be. Once we calculate these, we can put all of this information together in a nice three-dimensional surface plot.
We thus get:
Take note that both dimensions span from [-pi,pi]. If you examine the spectrum, you will see that the DC component gets nulled to 0, which is actually a high-pass filter, and not a sharpening filter. See my note below.
Minor note
BTW, your 2D filter definition is not a 2D sharpening filter. It is simply a 2D Laplacian, which is an edge detection and thus a high-pass filter. It only detects edges. If you want to properly sharpen the image, make sure you add 1 to the centre of the kernel, and so what you're really after is:
h2 = [-1 -1 -1; -1 9 -1; -1 -1 -1];
The offset of 1 will ensure that you are leaving the original signal untouched, but also adding the high-pass results on top of the original signal to thus sharpen your input.
To plot the frequency response of a 1D FIR filter, use freqz:
h = [-1, 3, -1]; %// FIR impulse response
N = 128; %// number of frequency samples
freqz(h,1,N); %// "1" because the filter is not recursive
The frequency axis is normalized to half the sample frequency, as usual.
See the documentation of freqz for further options.
To plot the frequency response of a 2D FIR filter, use freqz2:
h = [-1, -1, -1; -1, 8, -1; -1, -1, -1]; %// FIR impulse response (filter mask)
N = 128; %// number of frequency samples
freqz2(h,N,N)
The frequency axes are normalized to half the sample frequency, as in the 1D case.
See the documentation of freqz2 for further options.

Filters performance analysis

I am working on some experimental data which, at some point, need to be time-integrated and then high-pass filtered (to remove low frequency disturbancies introduced by integration and unwanted DC component).
The aim of my work is not related to filtering, but still I would like to analyze more in detail the filters I am using to give some justification (for example to motivate why I chosed to use a 4th order filter instead of a higher/lower one).
This is the filter I am using:
delta_t = 1.53846e-04;
Fs = 1/delta_t;
cut_F = 8;
Wn = cut_F/(Fs/2);
ftype = 'high';
[b,a] = butter(4,Wn,ftype);
filtered_signal = filtfilt(b,a,signal);
I already had a look here: High-pass filtering in MATLAB to learn something about filters (I never had a course on signal processing) and I used
fvtool(b,a)
to see the impulse response, step response ecc. of the filter I have used.
The problem is that I do not know how to "read" these plots.
What do I have to look for?
How can I understand if a filter is good or not? (I do not have any specification about filter performances, I just know that the lowest frequency I can admit is 5 Hz)
What features of different filters are useful to be compared to motivate the choice?
I see you are starting your Uni DSP class on filters :)
First thing you need to remember is that Matlab can only simulate using finite values, so the results you see are technically all discrete. There are 4 things that will influence your filtering results(or tell you if your filter is good or bad) which you will learn about/have to consider while designing a Finite response filter:
1, the Type of the filter (i.e. Hamming, Butterworth (the one you are using), Blackman, Hanning .etc)
2, the number of filter Coefficients (which determines your filter resolution)
3, the sampling frequency of the original signal (ideally, if you have infinite sampling frequency, you can have perfect filters; not possible in Matlab due to reason above, but you can simulate its effect by setting it really high)
4, the cut-off frequency
You can play around with the 4 parameters so that your filter does what you want it to.
So here comes the theory:
There is a trade-off in terms of the width of your main lobe vs the spectrum leakage of your filter. The idea is that you have some signal with some frequencies, you want to filter out the unwanted (i.e. your DC noise) and keep the ones you want, but what if your desired signal frequency is so low that it is very close to the DC noise. If you have a badly designed filter, you will not be able to filter out the DC component. In order to design a good filter, you will need to find the optimal number for your filter coefficients, type of filter, even cut-off frequency to make sure your filter works as you wanted.
Here is a low-pass filter that I wrote back in the days, you can play around with filters a lot by filtering different kinds of signals and plotting the response.
N = 21; %number of filter coefficients
fc = 4000; %cut-off frequency
f_sampling = fs; %sampling freq
Fc = fc/f_sampling;
n = -(N-1)/2:(N-1)/2;
delta = [zeros(1,(N-1)/2) 1 zeros(1,(N-1)/2)];
h = delta - 2*Fc*sinc(2*n*Fc);
output = filter(h,1,yoursignal);
to plot the response, you want to plot your output in the frequency domain using DFT or FFT(in Matlab) and see how the signal has been distorted due to the leakage and etc.
NFFT=256; % FFT length
output=1/N*abs(fft(output,NFFT)).^2; % PSD estimate using FFT
this gives you what is known as a periodigram, when you plot, you might want to do the 10*log10 to it, so it looks nicer
Hope you do well in class.

What type of filter is the "filter" command

I'm using MATLAB's filter function to create a moving average of a line graph. My question is, what kind of filter is? Low pass? High pass? etc...
I'm a sophomore electrical engineer so this kind of question is of particular interest to me.
I'm graphing the average temperature in January.
temperature = filter(ones(1,3)/3,1,janTemp);
plot(days,temperature);
where janTemp is the 1 column vector with the temperatures and days is the list of days in a vector.
filter doesn't create a filter; it applies a filter to a signal:
FILTER One-dimensional digital filter.
Y = FILTER(B,A,X) filters the data in vector X with the
filter described by vectors A and B to create the filtered
data Y.
The filter will be low-pass, high-pass etc depending on the coefficient vectors A, B. These vectors contain the coefficients of the difference equation defining the filter:
a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)
- a(2)*y(n-1) - ... - a(na+1)*y(n-na)
Equivalently, A and B define the numerator and denominator polynomials of the filter's transfer function, H(z).
In your case, with B = [1 1 1] and A = 1, it will be a low-pass FIR filter, i.e. a moving average, as you indicate in your question.
See filter's documentation or type help filter for details.
An easy way to visualize the frequency response of a filter is with freqz.
freqz([1 1 1]/3,1)
This filter attenuates high frequencies, but by no means very well.
Consider a 5-tap Butterworth FIR filter:
freqz(butter(5,0.5,'low'),1)
See also fvtool, the Filter Visualization Tool (FVTool).
Moving average is usually an example of low pass filter. See more information at http://en.wikipedia.org/wiki/Moving_average
In general you can learn more about filters in matlab at http://www.mathworks.com/tagteam/55876_digfilt.pdf.

MATLAB - Why doesn't HP+LP=NOTCH?

I'm having a conceptual issue with notch filters. As far as I understand it, a notch filter output is equal to the sum of highpass and lowpass filter outputs. However, a quick test in MATLAB doesn't show this. Here's some testing code:
% Load a simple signal and specify constants
load handel; % this loads the signal, y, and its sampling rate, Fs
nData=y;
nFreq=[55 65];
nOrder=4;
% Create a lowpassed signal
[b a]=butter(nOrder,nFreq(1)/(Fs/2),'low');
nLP_Data=filter(b,a,nData);
% Create a highpassed signal
[b a]=butter(nOrder,nFreq(2)/(Fs/2),'high');
nHP_Data=filter(b,a,nData);
% Combine LP and HP signals
nCombinedHPLP=nLP_Data+nHP_Data;
% Create a notch filtered signal
[b a]=butter(nOrder/2,nFreq/(Fs/2),'stop'); % The notch filter uses twice the first input argument for its order, hence the "/2"
nN_Data=filter(b,a,nData);
% Plot each and output the total difference in the signals to the Command Window
plot(nN_Data,'r')
hold all
plot(nCombinedHPLP,'b')
legend({'Notched signal','Combined HP, LP signals'});
sum(abs(nCombinedHPLP-nN_Data))
I'm thinking that the difference is due to phase effects of the filters, as changing filter to filtfilt yields two identical signals. Is there not a way (using `filter') of recreating the effect of a notch filter with a highpass and lowpass filter? Thanks for reading.
Rogare,
You HAVE created a notch filter; you just created one with different phase than what a single butter filter would do, as you suspected.
Both your BUTTER filters will have their own phase responses, and there's no guarantee that the processing of your data with both of those filters will have the same net phase as processing your data ONCE with a hand-made band-pass filter. There's no rule that says butter will spend time and energy trying to get the phase of a low-pass and a high-pass filter to behave like the phase of a band-pass filter (in fact, it would be weird if it did!)
However, the MAGNITUDE of the FFT of the resulting data agrees quite nicely:
subplot(2,1,1);
plot(abs(fftshift(fft(nCombinedHPLP))));
title('My Notch')
subplot(2,1,2);
plot(abs(fftshift(fft(nN_Data))));
title('Real Notch')
You do have a conceptual problem: in the opening paragraph of your question you say
As far as I understand it, a notch filter output is equal to the sum
of highpass and lowpass filter outputs
If that were the case, then any signal that passes either the lowpass or the highpass will get through (at low frequency, the lowpass lets it through, so the sum of the outputs of lowpass + highpass lets low frequencies through. Ditto for high frequencies, if you swap the words "low" and "high"...).
There's a difference between applying in series vs summing outputs. I think that is where your confusion is coming from. And yes - depending on how a filter is constructed, how steep it is, etc, you will get all kinds of effects when you apply them in series...
As the order of the notch filter is twice of the order of LP or HP filters, that should indicate that a NF can also be derived as conv(LP,HP).
The concept that you asking is that of superposition, which would presuppose equal length filters.