Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

load x_sound.

mat
% x_n = y';

n = -2500:2500;
x_n_1 = cos((pi/6) * n);
x_n_2 = cos((pi/2) * n);
x_n_3 = cos((5*pi/6) * n);
x_n = x_n_1 + x_n_2 + x_n_3;

g = length(x_n);
n = -g/2:g/2-1;
plot(n,abs(fftshift(fft(x_n))));
title('Frequency Domain Signal');

% Fs = 8000;
% input_sound = audioplayer(y,Fs);
% play(input_sound);
% audiowrite('input.wav',x_n,Fs)

h1 = fir1(64,1/3);
h2 = fir1(64,[1/3 2/3]);
h3 = fir1(64,2/3,'high');
freqz(h1,1,512);

1
freqz(h2,1,512);

2
freqz(h3,1,512);

y_1 = convme(x_n,h1);
y_2 = convme(x_n,h2);
y_3 = convme(x_n,h3);
g = length(y_1);
n = -g/2:g/2-1;
plot(n,abs(fftshift(fft(y_1))))

3
plot(n,abs(fftshift(fft(y_2))))

4
plot(n,abs(fftshift(fft(y_3))))

b = ones(1,g);
i = 1;
while i < g
b(1,i+1)=(-1)^i;
i = i + 1;
end

y_4 = y_1 .* b; % cos(pi*n) = -1^i


y_5 = y_3 .* b;
plot(n,abs(fftshift(fft(y_4))))

5
plot(n,abs(fftshift(fft(y_5))))

6
y_6 = y_4;y_7 = y_2;y_8 = y_5;
y_n = y_6 + y_7 + y_8;

plot(n,abs(fftshift(fft(y_n))),Color='b')

y_n_d = y_n(1,3:g); % delay by 2


plot(n,abs(fftshift(fft(y_n))),Color='b')

hold on
g = length(y_n_d);
n = -g/2:g/2-1;
plot(n,abs(fftshift(fft(y_n_d))),Color='r',LineStyle=':')
hold off

7
% Fs = 8000;
% input_sound = audioplayer(y_n,Fs);
% play(input_sound);
% audiowrite('out_to_TX.wav',y_n,Fs)

% bec its only delay by 2 the diff. is not obvious in


% long period so lets zoom in

hold on
g = length(y_n_d);
n = -g/2:g/2-1;
plot(n,abs(fftshift(fft(y_n_d))),Color='r',LineStyle=':')
xlim([415 430])
hold off

8
b = ones(1,g);
i = 1;
while i < g
b(1,i+1)=(-1)^i;
i = i + 1;
end

y_n_d_1 = y_n_d .* b; % cos(pi*n) = -1^i


y_n_d_2 = y_n_d;
y_n_d_3 = y_n_d .* b;

y_1_d = convme(y_n_d_1,h3);
y_2_d = convme(y_n_d_2,h2);
y_3_d = convme(y_n_d_3,h1);

% plot(n,abs(fftshift(fft(y_n_d_1))))
% plot(n,abs(fftshift(fft(y_n_d_2))))
% plot(n,abs(fftshift(fft(y_n_d_3))))
% d = length(y_1_d);
% n = -d/2:d/2-1;
% plot(n,abs(fftshift(fft(y_1_d))))
% plot(n,abs(fftshift(fft(y_2_d))))
% plot(n,abs(fftshift(fft(y_3_d))))

9
x_n_d = y_1_d + y_2_d + y_3_d;
d = length(x_n_d);
n = -d/2:d/2-1;
plot(n,abs(fftshift(fft(x_n_d))),Color='red')

% Fs = 8000;
% input_sound = audioplayer(x_n_d,Fs);
% play(input_sound);
% audiowrite('output.wav',x_n,Fs)

x = [1, 2, 3];
h = [2 ,3];

% this is a comparison between my_conv and matlab conv


% it is the same but the problem is that the output
% should be len(x) + len(h) but here the answer is
% less by 1 I do not know why....

y_original = conv(x,h)

y_original = 1×4
2 7 12 9

y_me = convme(x,h)

10
y_me = 1×4
2 7 12 9

function output = convme(X_n, b_coff)


X_n = [X_n zeros(1,length(b_coff))];

loop = length(X_n) ;
output = zeros(1, loop-1);

for n = 1:loop
Y_n = 0;
for k = 1:length(b_coff)
if n - k >= 1
Y_n = Y_n + b_coff(k) * X_n(n - k);
end
end

% Output the result


if n == 1
continue
else
output(1,n-1) = Y_n;
end
end
end

11

You might also like