Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.2 kB
1
Indexable
Never
f = 100; % frequency of the signal in Hz
T = 1/f; % period of the signal in s
t = 0:0.0001:5*T; % time vector
x = sin(2*pi*f*t); % signal

% Plot the continuous-time signal
figure(1)
plot(t,x)
xlabel('Time (s)')
ylabel('x(t)')
title('Continuous-time signal')

% Sample the signal at different sampling frequencies
fs1 = 150; % sampling frequency 1 in Hz
fs2 = 200; % sampling frequency 2 in Hz
fs3 = 300; % sampling frequency 3 in Hz
Ts1 = 1/fs1; % sampling period 1 in s
Ts2 = 1/fs2; % sampling period 2 in s
Ts3 = 1/fs3; % sampling period 3 in s
n1 = 0:Ts1:5*T; % sample index vector 1
n2 = 0:Ts2:5*T; % sample index vector 2
n3 = 0:Ts3:5*T; % sample index vector 3
x1 = sin(2*pi*f*n1); % sampled signal 1
x2 = sin(2*pi*f*n2); % sampled signal 2
x3 = sin(2*pi*f*n3); % sampled signal 3

% Plot the sampled signals
figure(2)
subplot(3,1,1)
stem(n1,x1)
hold on
plot(n1,x1)
hold off
xlabel('Time (s)')
ylabel('x[n]')
title(['Sampled signal 1 with fs = ',num2str(fs1),' Hz'])
subplot(3,1,2)
stem(n2,x2)
hold on
plot(n2,x2)
hold off
xlabel('Time (s)')
ylabel('x[n]')
title(['Sampled signal 2 with fs = ',num2str(fs2),' Hz'])
subplot(3,1,3)
stem(n3,x3)
hold on
plot(n3,x3)
hold off
xlabel('Time (s)')
ylabel('x[n]')
title(['Sampled signal 3 with fs = ',num2str(fs3),' Hz'])

% Reconstruct the signal using sinc interpolation
xr1 = zeros(size(t)); % reconstructed signal 1
xr2 = zeros(size(t)); % reconstructed signal 2
xr3 = zeros(size(t)); % reconstructed signal 3
for i = 1:length(n1)
    xr1 = xr1 + x1(i)*sinc((t-n1(i))/Ts1); % sinc interpolation formula
end
for i = 1:length(n2)
    xr2 = xr2 + x2(i)*sinc((t-n2(i))/Ts2); % sinc interpolation formula
end
for i = 1:length(n3)
    xr3 = xr3 + x3(i)*sinc((t-n3(i))/Ts3); % sinc interpolation formula
end

% Plot the reconstructed signals
figure(3)
subplot(3,1,1)
plot(t,xr1)
xlabel('Time (s)')
ylabel('xr1(t)')
title(['Reconstructed signal 1 with fs = ',num2str(fs1),' Hz'])
subplot(3,1,2)
plot(t,xr2)
xlabel('Time (s)')
ylabel('xr2(t)')
title(['Reconstructed signal 2 with fs = ',num2str(fs2),' Hz'])
subplot(3,1,3)
plot(t,xr3)
xlabel('Time (s)')
ylabel('xr3(t)')
title(['Reconstructed signal 3 with fs = ',num2str(fs3),' Hz'])
Leave a Comment