data_width = 18; % Data input bit width
D = 4; % Interpolation factor
max_data = 2^(data_width-1)-1;
min_data = -max_data;
% Filter Coefficients
coef=[-702 -22825 -32871 -21572 11845 58648 103632 131071 131071 103632 58648 11845 -21572 -32871 -22825 -702];
coef1=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16];
%Impulse response
Inp = zeros(300,1);
Inp(10)=1;
%Impulse Response Plotting
figure(1);
output = resample(Inp, 1, D, coef1);
impulse_output = output;
sz = size(output);
max_x = max(sz);
xline = linspace (0, max_x - 1, max_x);
plot (xline, output,'r');
title ('Time Display of Impulse Response');
grid on;
zoom on;
outfile1 = fopen('imp_in.txt','w');
fprintf(outfile1, '%d\n', Inp);
fclose(outfile1);
outfile1 = fopen('m_Inpulse_out.txt','w');
fprintf(outfile1, '%d\n', output);
fclose(outfile1);
% Frequency Domain Plotting
to_plot = output;
figure(2);
freqdat = fft(to_plot);
absdat = abs(freqdat);
maxdat = max (absdat);
logdat = 20*log10(absdat);
sz = size(to_plot);
numpts = max(sz);
freq_res = 1/numpts;
xline = linspace (0, ( (1/2)- freq_res ), round(numpts/2) );
plot (xline, logdat(1: round(numpts/2) ), 'b');
title ('Frequency Display of Impulse Response');
grid on;
zoom on;
xlabel ('Frequency');
ylabel ('Magnitude - dB');
%Step Response
step= zeros(300,1);
for i=10:13
step(i) = 1;
end;
%step Response Plotting
figure(3);
output = resample(step, 1, D, coef1);
step_output = output;
sz = size(output);
max_x = max(sz);
xline = linspace (0, max_x - 1, max_x);
plot (xline, output,'r');
title ('Time Display of Step Response');
grid on;
zoom on;
outfile2 = fopen('step_in.txt','w');
fprintf(outfile2, '%d\n', step);
fclose(outfile2);
outfile2 = fopen('m_Step_out.txt','w');
fprintf(outfile2, '%d\n', output);
fclose(outfile2);
% Frequency Domain Plotting
to_plot = output;
figure(4);
freqdat = fft(to_plot);
absdat = abs(freqdat);
maxdat = max (absdat);
logdat = 20*log10(absdat);
sz = size(to_plot);
numpts = max(sz);
freq_res = 1/numpts;
xline = linspace (0, ( (1/2)- freq_res ), round(numpts/2) );
plot (xline, logdat(1: round(numpts/2) ), 'b');
title ('Frequency Display of Step Response');
grid on;
zoom on;
xlabel ('Frequency');
ylabel ('Magnitude - dB');
%Ramdom Input
random = round((rand(1, 100)-0.5)*(2^data_width-2)-1);
int_random = round(random);
zero_init = zeros(10,1);
zero_init = zero_init';
int_random = [zero_init int_random];
% Saturating Input Value
a_in = find (int_random > max_data);
b_in = find (int_random < min_data);
if (~isempty(a_in)|~isempty(b_in))
lenax = length (a_in);
lenbx = length (b_in);
for i = 1:lenax
int_random(a_in(i)) = max_data;
end
for i = 1:lenbx
int_random(b_in(i)) = min_data;
end
end
%Random Response Plotting
int_random = [0 0 0 0 0 0 0 0 0 0 -15784 -41931 -48702 -35370 -27987 23992 -99681 -121077 -10854];
figure(5);
output = resample(int_random, 1, D, coef);
random_output = output;
sz = size(output);
max_x = max(sz);
xline = linspace (0, max_x - 1, max_x);
plot (xline, output,'r');
title ('Time Display of random Response');
grid on;
zoom on;
outfile3 = fopen('rand_in.txt','w');
fprintf(outfile3, '%d\n', int_random);
fclose(outfile3);
outfile3 = fopen('m_Random_out.txt','w');
fprintf(outfile3, '%d\n', output);
fclose(outfile3);
% Frequency Domain Plotting
to_plot = output;
figure(6);
freqdat = fft(to_plot);
absdat = abs(freqdat);
maxdat = max(absdat);
logdat = 20*log10(absdat);
sz = size(to_plot);
numpts = max(sz);
freq_res = 1/numpts;
xline = linspace (0, ( (1/2)- freq_res ), round(numpts/2) );
plot (xline, logdat(1: round(numpts/2) ), 'b');
title ('Frequency Display of Random Response');
grid on;
zoom on;
xlabel ('Frequency');
ylabel ('Magnitude - dB');