%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Creative Commons
% Attribution-Noncommercial 2.5 India
% You are free:
% to Share — to copy, distribute and transmit the work
% to Remix — to adapt the work
% Under the following conditions:
% Attribution. You must attribute the work in the manner 
% specified by the author or licensor (but not in any way 
% that suggests that they endorse you or your use of the work). 
% Noncommercial. You may not use this work for commercial purposes. 
% For any reuse or distribution, you must make clear to others the 
% license terms of this work. The best way to do this is with a 
% link to this web page.
% Any of the above conditions can be waived if you get permission 
% from the copyright holder.
% Nothing in this license impairs or restricts the author's moral rights.
% http://creativecommons.org/licenses/by-nc/2.5/in/

% Checked for proper operation with Octave Version 3.0.0
% Author	: Krishna
% Email		: krishna@dsplog.com
% Version	: 1.0
% Date		: 2 December 2007
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Script for a digital implementation of RC low pass filter
close all
fsMHz = 10; % frequency, MHz
ip = [zeros(1,25) ones(1,75) zeros(1,25)]; % step input
N = 4096; % Number of points for frequency analysis 

k = 0.1;
[h1F f1] = freqz([0 k],[1 -(1-k)],N,'whole',fsMHz);
op1 = filter([0 k],[1 -(1-k)],ip);

k = 0.2;
[h2F f2] = freqz([0 k],[1 -(1-k)],N,'whole',fsMHz);
op2 = filter([0 k],[1 -(1-k)],ip);

figure
plot([-N/2:N/2-1]*fsMHz/N,20*log10(abs(fftshift(h1F))),'m.-');
hold on
plot([-N/2:N/2-1]*fsMHz/N,20*log10(abs(fftshift(h2F))),'c.-');
axis([-fsMHz/2 fsMHz/2 -25 3])
grid on
xlabel('frequency, MHz')
ylabel('20log10(abs(H)))')
title('Magnitude response of the filter')
legend('k=0.1', 'k=0.2') 

figure
plot([0:length(ip)-1]/fsMHz,ip,'b.-');
hold on
plot([0:length(op1)-1]/fsMHz,real(op1),'m.-');
hold on
plot([0:length(op2)-1]/fsMHz,real(op2),'c.-');
xlabel('time, \mus')
ylabel('amplitude');
title('step response')
grid on
axis([0 12.4 0 1.2])
legend('ip', 'op1 k=0.1', 'op2 k=0.2')






