% sequential direct form II IIR implementation test
% 2 delays
% 6 multiplications (worst case)
clear all

RC = 1;
alph = 1/(RC + 1);

input = zeros(1000,1);
input(1) = 1;
output = zeros(1000,1);
f = linspace(-1,1,length(input)); % normalized frequency

for n=2:length(input)
   output(n) = output(n-1) * (1-alph) + input(n-1) * alph;
end

subplot(3,1,1)
stem(input)
hold on
grid on
stem(output,'r')
xlim([0 30])
title('Impulse Response')
xlabel('sample no.')
ylabel('amplitude')

subplot(3,1,2)
plot(f, 20*fftshift(log10(abs(fft(input)))))
hold on
grid on
plot(f, 20*fftshift(log10(abs(fft(output)))),'r')
title('Frequency Plot');
xlabel('normalized frequency')
ylabel('magnitude in dB')

subplot(3,1,3)
plot(f, fftshift(angle(fft(input))))
hold on
grid on
plot(f, fftshift(angle(fft(output))),'r')
title('Phase Plot');
xlabel('normalized frequency');
ylabel('phase in rad')

