MATLAB Programming/Advanced Topics/Applications and Examples/Filtering< MATLAB Programming | Advanced Topics/Applications and Examples(Redirected from MATLAB Programming/Filtering)
Filtering is a broad subject. For the MATlab wiki I will focus on how to implement filters. For more on the theory of filtering the reader should reference the Digital Signal Processing wiki book.
The Moving Average FilterEdit
MATLAB implementation(All the code here was intended to be put in an M-file):
clc; clear; % clear all v=.01 f=100; fs=5000; t=0:1/fs:.03 x=sin(2*pi*f*t); %original signal r=sqrt(v)*randn(1,length(t)); %noise Xw=x+r; %signal plus noise (filter input) % I have chosen h=3 for n=3:length(Xw), y(n)=sum(Xw(n-2:n))/3; %y[n] is the filtered signal end plot(y); hold; plot(x,'r'); %plot the original signal over top the %filtered signal to see the difference
The moving average filter is simple and effective. One of the things that is a problem is the lag associated with the moving average filter. The more samples used the longer the lag experienced(All filters have lag). How much lag can be tolerated is up to the individual.
The Alpha Beta filterEdit
The Kalman FilterEdit
The Kalman filter is a recursive method of combining two estimates to determine the truth. A few parameters that are widely used are the initial conditions or current value and measured data.
n=100; sigma=(20/6076); R=100; Rm=R+sigma*randn; Rs(1)=Rm(1); Cs=sigma^2 for i=2:n Rm(i)=R+sigma*randn; alpha=Cs/(Cs+sigma^2); Rs(i)=Rs(i-1)+alpha*(Rm(i)-Rs(i-1)); Cs=(1-alpha)*Cs; end
All this code does is take a constant value R and adds noise to it. Then it filters the new signal in an effort to separate the noise from the original signal.
The discrete Fourier transformEdit
|What is it?||DFT element||matlab example and comments|
|How often do you want to sample?||sampling frequency||
|For how long do you want to sample?||time range||
|How many samples does that give you?||
|How far apart are each of the frequency-domain result points?||
|What signal do you want to sample?||input||
|What are the results?||Fourier transform||fft_x=fft(x, length(x));|
|What frequencies does the signal have?||fft_x_mag=abs(fft_x);|
|What phase relationships?||fft_x_phase=unwrap(angle(fft_x));|
|How do you view the results?||
|What about the power spectrum?||
Lyons, Richard G. Understanding digital signal processing. Upper Saddle River: Prentice Hall PTR, 2001. ISBN 0-201-63467-8. Chapter 3 discusses the DFT.