%This program is used for computing raised cosine impulse response modified
%by Hamming window
clc
clear all
close all

%Warning: While entering file names please enclose the file name within ''
file_1 = input('ENTER NAME FOR  [t, g(t)] LIST FILE: ');
file_2 = input('ENTER FILENAME FOR g(t) SORTED AS SUBFILTERS: ');

fs = input('ENTER SYMBOL RATE, FS, IN HZ: ');
M = input('ENTER NUMBER OF SAMPLES/BAUD, M : ');
L = input('ENTER TRUNCATION LIMIT L (+/- L BAUDS): ');

ML = M*L;
G = zeros(2*ML+1,1); 
%G is the array to hold impulse response samples
alpha = input('ENTER EXCESS BANDWIDTH FACTOR, ALPHA : ');
% alpha is the excess bandwidth factor for raised cosine response


for k = -ML:ML
     t = k/M;
     G(k+ML+1) = h_impulse(t,alpha);
     G(k+ML+1) = G(k+ML+1)*(0.54 + 0.46*cos(2*pi*k/(2*ML+1)));
     %The impulse response is multiplied by the Hamming window
end

t = -L:(1/M):L;
G_format = zeros(2*ML+1,2);
G_format(:,1) = t';
G_format(:,2) = G;
figure(1);
plot(t',G);
grid on;
title('Plot of the impulse response of raised cosine filter');

file_1 = strcat(file_1,'.txt');
fid_1 = fopen(file_1,'wt');
fprintf(fid_1,'Values of impulse response of raised cosine filter for different time instants\n\n');

for i = 1:2*ML+1
    fprintf(fid_1, '%f\t%E', G_format(i,1), G_format(i,2));
    fprintf(fid_1,'\n');
end
fclose(fid_1);

file_2 = strcat(file_2,'.txt');
fid_2 = fopen(file_2,'wt');
fprintf(fid_2,'In g[i][j], i is the subfilter index, j is the coefficient index in subfilter\n\n');

for i = 1:M
    for j = 1:(2*L)
        fprintf(fid_2,strcat('g[',num2str(i-1),'][',num2str(j-1),'] ='));
        fprintf(fid_2,'%E',G(M*(j-1)+i));
        fprintf(fid_2,' \n');
    end
    fprintf(fid_2,' \n');
end

fclose(fid_2);