% file e310dem2a.m
% This program implements the filters of Assignment 1
% and uses real data (voice, music, image)

% =======================================
% MATLAB HELP
% to learn more about Matlab, type 'intro'
% (without quotes) at the command prompt
% also type 'demo' for more complete intro
% for help on any function, type 'help functionname'
% for help on syntax:
% type 'help punct' 'help ops'  'help arith'
% for web-based help, type 'helpdesk'
% keyword search, type 'lookfor keyword'

% use round brackets for subscripts b(1)
% vector index must be 1 or greater
% use square brackets for vectors [b(1) b(2) b(3)]

% data files (voice, music, image)
% copy these files, or put in your path using e.g.
% data is loaded using 'load filename' 
% (without the *.mat extension)

% consider system with input x(k), filter h(k), output y(k)

% ===============
% load real data 
% ===============
% choose one of voice, music, image
% comment out the others
% this file is set up for image

% voice
% load lab6dat; % x2 is the voice data
% x2 is a vector of lenght 69797
% type 'whos' to see all the variables in lab6dat.mat
% x=x2;

% music
% load furelise; % FE is the music data
% FE is a vector of length 69797
% x=FE;

% any .wav audio file
% x=wavread('filename.wav');
% any .au audio file (unix)
% x=auread('filename.au');

% image
 load lenna; % xx is the image data
% xx is a matrix of 256 by 256 pixels
x = xx;

% =============
% play sound or display image
% =============
% choose one, comment out other

% soundsc(x); % plays audio (voice or music)
show_img(x,1); % displays image as figure 1


% now filter the data
% choose of of 3 filters (question 1,2,3)
% comment out the others to save compute time

% ============
% question 1
% ============

% define filter coefficient vector h
%h=[0.5 1 0.5];
h=ones(1,16);
% same as h=[1 1 1 ... 1] vector of 16 ones

b=h;
% find output vector y using
% y(n) = b(0)*x(n) + b(1)*x(n-1) + b(2)*x(n-2)
% but index k for the vector b must be shifted up by 1, so write
% y(n) = b(1)*x(n) + b(2)*x(n-1) + b(3)*x(n-2)
y=filter(b,1,x);
% type 'help filter' to see the
% equations for the filter function
% impulse response h is same as b in this case
  
% ============
% question 2
% ============
% define filter coefficient vector b
h2=[1 -1];
b2=h2;
% find output vector y using
% y(k) = b(1)*x(k) + b(2)*x(k-1)
y2=filter(b2,1,x);
% impulse response h2 is same as b2 in this case 

% ===========
% question 3
% ===========
% define filter coefficient vectors a and b
a=[1 -0.9]; % a = [a(1) a(2)] 
b3=[0 1];
% find output vector y using
% y(k)=0.9*y(k-1) + x(k-1)  (shifted the a index up by one for matlab)
y3=filter(b3,a,x); % type 'help filter' to see equations
% can also write a(1)*y(k) + a(2)*y(k-1) = b(1)*x(k) + b(2)*x(k-1)
% or y(k) - 0.9*y(k-1) = x(k-1)
% so a(1) = 1, a(2) = -0.9, b(1) = 0, b(2) = 1
% or a = [1 -0.9], b=[0 1]

% =============
% play sound or display image
% =============
% choose one, comment out others

% soundsc(y); % plays filtered sound
% wavwrite(y); % writes .wav file
% auwrite(y); % write .au file (unix)

show_img(y,2); % displays image as figure 2
show_img(y2,3); % displays image as figure 3
show_img(y3,4); % displays image as figure 4
