Demonstration of an Oversampling FIR Filter

From Class Wiki
Jump to navigation Jump to search

This is a demo of oversampling. Setting average = true, you can use the three term average filter, , or with average = false, you get a filter derived from the inverse Fourier transform of a brick wall filter.

Note that the "brick wall" filter (average=false) has a problem where the points that start at zero don't come up high enough. For example, try M=1, N=2, and average = false. What is this from and how do you suppose you might fix this?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Coders: Nory Salsbery, Carlos Flores, Austin von Pohle
%
% Class: Signals and Systems (Hang out With Dr. Frohne Time)
%
% Description: This is an FIR filter that over samples by 
% a variable amount.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all
clear all
clf;
average = false;
%Oversamples, N, (Integers Only)
N=2; 
sum=0;
% Sample rates
Ts=1/1000; 
%Filter Sample Rate
T=(1/N)*Ts; 
Totaltime = .1;
% For the frequency response
delta_f=1/(1000*Ts);
f=-1/T:delta_f:1/T;
if (average)
  display("Using Cheesy Average Filter")
  hf=[.5,1,.5];
  sum=1+cos(pi*f*T);
  M=1;
  N=2;
else
  %Discrete number of sample points
  M=1;
  %Creating Filter Funtion
  hf=[];
  for m=-M:M;
	  if (m==0)
  		h=1/N;
	  else
      h=sin(pi*m/N)/(pi*m);
    endif
    hf=[hf h];
    sum=sum+h*exp(-i*2*pi*f*m*T);
  endfor
endif
%Filter impulse response.
plot(hf,'r*')
title('Filter impulse response')
xlabel('n')
%Frequency Response of our filter
figure()
plot(f,20*log10(abs(sum)))
title('Frequency Response of Our FIR Filter')
xlabel('Frequency (Hz)')
ylabel('Response (db)')
text(-1.5,-5,['M = ',num2str(M)])
% We want to see what this looks like.
% Make a signal that is swept in frequency
t = 0:Ts:Totaltime;
f=600*t;%Frequency of Sandy Patty
%Sandy Patty.
xs = sin(2*pi*f.*t);
figure() % Plots Sandy Patty
scatter(t,xs)
xn=zeros(1,N*length(xs));
xn(1:N:length(xn))=xs(1:1:length(xs));
% FIR filter (This code shows how simple it is.)
hp=fliplr(hf);
for j=2*M+1:length(xn)
  y(j-2*M)=xn(j-2*M:j)*hp';
endfor
% Or filter with Octave's built in function. (This method is faster.)
%y=filter(real(hf),1/N,xn);
%t2 = 0:T:(Totaltime+(N-1)*T);
t2 = 0:T:T*(length(y)-1);
figure()%Plot Filtered Sandy Patty
scatter(t2,y)
title('Oversampled Signal')
xlabel("time (s)")