Supporting Functions

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

Câu 1

clc
clear all
%tin hieu x[n]
x = [1 2 3 4 5 6 7 8 9 10 11 12];
nx = 0:11;

%ve tin hieu x[n]


figure(1)
subplot(2,4,1)
stem(nx,x);
title('x[n]');
xlabel('n');
ylabel('Amplitude');
grid on;

%1a
%dich x(n+3)
[s1, ns1] = shift(x,-3, nx);
subplot(2,4,2)
stem(ns1, s1)
grid on
title('x[n+3]')
xlabel('n', 'fontsize', 12)
ylabel('s[n]', 'fontsize', 12)

%1b
%ve tin hieu x[n-2]
[x1,n1] = shift(x,2,nx);
subplot(2,4,3);
stem(n1,x1);
title('x[n-2]');
xlabel('n');
ylabel('Amplitude');
grid on;
%ve tin hieu x[-n-2]
[x2,n2] = reverse(x1,n1);
subplot(2,4,4);
stem(n2,x2);
title('x[-n-2]');
xlabel('n');
ylabel('Amplitude');
grid on;
%ve tin hieu x[-2n-2]=v[n]
scale = 2;
x3 = x2(1:scale:end)
n3 = (n2(1:scale:end))/scale

subplot(2,4,5);
stem(n3,x3);
title('x[-2n-2]');
xlabel('n');
ylabel('Amplitude');
grid on;

[x4,n4]=reverse(x3,n3);
subplot(2,4,8);
stem(n4,x4);
title('v[-n]');
xlabel('n');
ylabel('Amplitude');
grid on;

scale = 1/2;
x5 = x4(1:scale:end)
n5 = (n4(1:scale:end))/(1/2)
subplot(2,4,6);
stem(n5,x5);
title('v[-n/2]');
xlabel('n');
ylabel('Amplitude');
grid on;

%cau c, tinh tich chap y[n]=x[n]*v[n]


[s4, ns4] = convolve(x, nx, x3, n3)
subplot(2,4,7);
stem(ns4,s4);
grid on
title('x[n]*v[n]')
xlabel('n','fontsize',12)
ylabel('function','fontsize',12)
Câu 2
%tao cac ham
n = 0:50;
% T?o tín hi?u delta (impulse) t?i n = 0
impulse = zeros(size(n));
impulse(1) = 1;

% T?o tín hi?u b??c (step) và tín hi?u ramp


step = zeros(size(n));
step(n >= 0) = 1;
step(n < 0) = 0;

ramp = n .* step;

figure(1)
subplot(3, 1, 1);
stem(n, step, 'filled');
title('tin hieu buoc nhay don vi u[n]');
xlabel('n');
ylabel('u[n]');
grid on;

subplot(3, 1, 2);
stem(n, impulse, 'filled');
title('tin hieu xung don vi f[n]');
xlabel('n');
ylabel('f[n]');
grid on;

subplot(3, 1, 3);
stem(n, ramp, 'filled');
title('tin hieu ramp r[n]');
xlabel('n');
ylabel('r[n]');
grid on;

%2a
[r, nr] = shift(ramp,-3, n);
v = r .* step;

figure(2)
subplot(3,1,1);
stem(nr, v)
grid on
title('v[n]')
xlabel('n')
ylabel('v[n]')

%2b

[sigma, nsigma] = shift(impulse, -6, n);


[sigma1, v1, nb] = compsig(sigma, nsigma, v, nr);
x = sigma1 .* v1;
subplot(3,1,2);
stem(nb, x)
grid on
title('x[n]')
xlabel('n', 'fontsize', 12)
ylabel('x[n]', 'fontsize', 12)

% 3_iii
% T?o tín hi?u m? (exponential)
exp = (1/6).^n .* step;

% 3_c
% D?ch chuy?n tín hi?u m? và tính toán y
[e, ne] = shift(exp, -10, n);
[e1, step1, nc] = compsig(e, ne, step, n);
y = e1 .* step1;

% V? ?? th? cho y[n]


figure(2)
subplot(3,1,3);
stem(nc, y)

grid on
title('y[n]')
xlabel('n', 'fontsize', 12)
ylabel('s[n]', 'fontsize', 12)

Câu 2 pt
n = 0:50; % Range for n

% unit step signal


u = ones(size(n));
u(n < 0) = 0;
step(n >= 0) = 1;

% delta (impulse)
impulse = zeros(size(n));
impulse(1) = 1;
% ramp signal
r = n .* u;

% Generate v[n] = r[n+3] * u[n]


v = zeros(size(n));
for i = 1:length(n)
if n(i) >= 0
v(i) = (n(i) + 3) * u(i);
end
end

% Generate x[n] = delta[n+6] * v[n]


% T?o tín hi?u x[n] = delta[n+6] * v[n]
x = zeros(size(n));
if any(n == -6)
delta_n6_index = find(n == -6);
x(delta_n6_index) = 1 * v(delta_n6_index);
end

% Plot the signals


figure(2);
subplot(3,1,1);
stem(n, v);
title('v[n] = r[n+3] * u[n]');
xlabel('n');
ylabel('v[n]');

subplot(3,1,2);
stem(n, x);
title('x[n] = \delta[n+6] * v[n]');
xlabel('n');
ylabel('x[n]');

figure(1)
subplot(3, 1, 1);
stem(n, u);
title('tin hieu buoc nhay don vi u[n]');
xlabel('n');
ylabel('u[n]');
grid on;

subplot(3, 1, 2);
stem(n, impulse);
title('tin hieu xung don vi f[n]');
xlabel('n');
ylabel('f[n]');
grid on;

subplot(3,1,3);
stem(n, r);
title('Ramp signal r[n]');
xlabel('n');
ylabel('r[n]');

Câu 3

%Thiet ke bo loc FIR, bài 3


u = pi/3;
l = pi/6;
[y68,ny68] = FIRdesign(u, l, 68);
[h1, w1] = freqz(y68, 1, 512);
mag1 = abs(h1);
rad2deg = 180/pi;
ang_h1 = wrapTo180(unwrap(angle(h1))*rad2deg);
%Plot Magnitude response
figure(1)
subplot(2,1,1)
plot(w1,mag1)
grid on
title('The magnitude of the frequency response of N=68',
'fontname', 'Comic Sans MS', 'fontsize',14)
xlabel('\omega', 'fontname', 'Comic Sans MS', 'fontsize',
14)
ylabel('Amplitude', 'fontname', 'Comic Sans MS', 'fontsize',
14)
% plot phase response

subplot(2,1,2)
plot(w1,ang_h1)
grid on
title(' The angle of the frequency response of N=68',
'fontname', 'Comic Sans MS', 'fontsize', 14)
xlabel('\omega', 'fontname', 'Comic Sans MS', 'fontsize',
14)
ylabel('Amplitude', 'fontname', 'Comic Sans MS', 'fontsize',
14)

Shift
function [s, n] = shift(x, N, nx)
% Shift the signal by N samples
s = x;
n = nx + N;
end

Reverse
function [r, k] = reverse(x, nx)
% Reverse the signal and update the time indices
r = x;
k = -nx;
end

FIR
% Function to design a finite impulse response frequency
selective filter
function [y, ny] = FIRdesign(wl, wu, N)
% Create a one-sided impulse response
for n = 1:2*N
h(n) = (sin(wu * n) - sin(wl * n)) / (pi * n);
end
n = [1:2*N];
% Reverse the impulse response
[h_r, n_r] = reverse(h, n);
[h_comp, h_r_comp, n_comp] = compsig(h, n, h_r, n_r);
h_comp = h_comp + h_r_comp;
% Create a boxcar window
for n = 1:N
box(n) = 1;
end
n_box = -(N - 1) / 2:(N - 1) / 2;
% Convolve the impulse response and the window
[y, ny] = convolve(h_comp, n_comp, box, n_box);
end
Convolve
%convolution function
function [y, ny] = convolve(h, nh, x, nx)
[x_r, nx_r] = reverse (x, nx);
k = length(nh);
x_r = [x_r zeros(1,k-1)];
y = zeros(1, length(x_r));
n = 0;
while(k>=0)
y = y + x_r.*h(find(h,1,'first')+n);
k = k-1;
x_r = circshift(x_r, [k 1])
x_r(1) = 0;
if(find(h, 1, 'first')+n == find(h, 1, 'last'));
k = -1;
end
n = n + 1;
end
ny = min(nh)-length(x)+1:max(nh);
end

Comsig
function [s1, s2, n] = compsig(x1, n1, x2, n2)
% Determine the minimum and maximum time indices
nmin = min([min(n1), min(n2)]);
nmax = max([max(n1), max(n2)]);
n = nmin:nmax; % Time indices are set.
nsize = size(n, 2);
[s1, s2] = deal(zeros(1, nsize));
x1size = size(x1, 2);
x2size = size(x2, 2);
x1first = find(n == n1(1));
x2first = find(n == n2(1));
switch (n1(1) < n1(2))
case true
s1(x1first : x1first + x1size - 1) = x1;
otherwise
s1(x1first - x1size + 1 : x1first) = fliplr(x1);
end
switch (n2(1) < n2(2))
case true
s2(x2first : x2first + x2size - 1) = x2;
otherwise
s2(x2first - x2size + 1 : x2first) = fliplr(x2);
end
end

window

%Box
for n = 1:N
box(n) = 1;
end
n_box = -(N-1)/2:(N-1)/2;
%Bartlett
n=1;
for i = -(N-1)/2:0
temp1(n)=2*i/(N-1)+1;
n = n+1;
end
n = [-(N-1)/2: 0];
m=1;
for j = 0:(N-1)/2
temp2(m)=1-2*j/(N-1);
m = m+1;
end
m=[0:(N-1)/2];
[s1, s2, n] = compsig(temp1, n, temp2, m);
s1(find(s1==1)) = s1(find(s1==1))-1;
bartlett = s2 + s1;
%Hanning
n=1;
for i = -(N-1)/2:(N-1)/2
hanning(n)=0.5+0.5*cos(2*pi*i/(N-1));
n = n+1;
end
n = [-(N-1)/2: (N-1)/2];
%Hamming
n=1;
for i = -(N-1)/2:(N-1)/2
hamming(n)=0.54+0.46*cos(2*pi*i/(N-1));
n = n+1;
end
n = [-(N-1)/2: (N-1)/2];
%Blackman
n=1;
for i = -(N-1)/2:(N-1)/2
blackman(n)=0.42+0.5*cos(2*pi*i/(N-1))+0.08*cos(4*pi*i/
(N-1)); n = n+1;
end
n = [-(N-1)/2: (N-1)/2];

You might also like