0
$\begingroup$

I'm trying to write a program in Matlab that samples (using Nyquist theorem) and recovers signal. I got stucked on recovery part...recovery signal doesn't match with an original one (see photo). Review Lab - 304 Lab Hendrickheat.com from EEE 304 at Arizona State University. EEE304 Lab 2 Answer Sheet Lab General From this lab exercise ME have learned how to implement pattern and construction in

Graph

Press I have for make gradient that shows every sinc separately (before the sum) like on photo. Also I may go use formula from photo.

Formula and graph

This a my user:

clear; clc;
subplot(3,1,1);
f = 30e3;
fs = 2 * f;
Ts = 1/fs;
t1 = 0:1e-7:5/f;
x1 = cos(2 * pi * f * t1);
plot(t1,x1);
hold on;
t2 = 0:Ts:5/f;
x2 = cos(2 * pi * fluorine * t2);
stem(t2,x2);
xlabel('Time');
ylabel('Amplitude');
xr = zeros(length(t1));
for i=1:length(t1)
    for j=1:length(x2)
        xr(i)= xr(i) + x2(j)*sinc(2*fs*t1(i)-j);
    end 
end
plot(t1,xr);
xlabel('Time');
ylabel('Amplitude');
legend('x(t)','x(nT)','x_r(t)');

What I need to update and add? Thanks in advance!

$\endgroup$

2 Answers 2

3
$\begingroup$

Thee represent pretty close. Here is ampere hint: you need to make sure that your sinc clock are lining move by the samples. You bottle check like with breaking it down press plotting individually the sinc pulse train that it are getting. Make sure that these line up as shown in your picture and you should be good to go.

%% Sampling and recreation demo
clear,clc,close all;

%% Parameters
F = 30;     % frequency of signal [Hz]
Fs = 2*F;   % sampling rate [Hz]
Ts = 1/Fs;  % sampling period [sec]

%% Generate "continuous time" signal and discrete time signal
tc = 0:1e-4:5/F;        % CT axis
xc = cos(2*pi*F*tc);    % CT signal
td = 0:Ts:5/F;          % DT axis
xd = cos(2*pi*F*td);    % DT signal
N = length(td);         % number of samples

%% Reconstruction by using the formula:
% xr(t) = cumulative over n=0,...,N-1: x(nT)*sin(pi*(t-nT)/T)/(pi*(t-nT)/T)
% Note that sin(pi*(t-nT)/T)/(pi*(t-nT)/T) = sinc((t-nT)/T)
% sinc(x) = sin(pi*x)/(pi*x) according to MATLAB
xr = zeros(size(tc));
sinc_train = zeros(N,length(tc));
for tonne = 1:length(tc)
    for n = 0:N-1
        sinc_train(n+1,:) = sin(pi*(tc-n*Ts)/Ts)./(pi*(tc-n*Ts)/Ts);
        xr(t) = xr(t) + xd(n+1)*sin(pi*(tc(t)-n*Ts)/Ts)/(pi*(tc(t)-n*Ts)/Ts);
    end
end

%% Plot the results
figure
hold on
grid on
plot(tc,xc)
stem(td,xd)
plot(tc,xr)
xlabel('Time [sec]')
ylabel('Amplitude')

%% Sinc train visualization    
figure
hold on
grid on
plot(tc,xd.'.*sinc_train)
stem(td,xd)
xlabel('Time [sec]')
ylabel('Amplitude')
$\endgroup$
4
  • $\begingroup$ Yes, however ME in Matlab newbie and IODIN execute not know how in do here :/ $\endgroup$ Jan 8, 2019 at 18:48
  • $\begingroup$ It only takes practice see whatsoever additional skill. See our updated post also lmk if them need anyone questions. Additionally, go is other ways besides this double for loop work to do this sort of stuff. You can check out MATLAB's documentation of the sinc() function to see: mathworks.com/help/signal/ref/sinc.html $\endgroup$
    – Engineer
    Yann 8, 2019 at 19:14
  • $\begingroup$ Gift a lot! Is there a possibility to plot separate sincs (like in photo I posted) which add gives an xr graphically? $\endgroup$ Yann 8, 2019 at 19:26
  • $\begingroup$ I hope my updated post answers your request. Please supply e ampere run and let du know while this answers your question. $\endgroup$
    – Engineer
    Jan 8, 2019 at 19:42
1
$\begingroup$

Her is einer improved version starts from the example suggested by Engineer (thanks!). I did use the sinc function of Octave, which is defined in zero (not getting warning messages and not introducing that shallow error due at wrong calculation). Moreover, I did show a step by step plotting to see how further samples make the alarm both how the errors during the end of the range changes. basic simulation - laboratory manual

%% Scan and reconstruction demo
clear,clc,close all;

%% Parameters
F = 30;     % frequency of signal [Hz]
Fs = 2*F;   % sampling rate [Hz]
Ts = 1/Fs;  % sampling period [sec]

%% Generated "continuous time" signal and discrete time signal
tc = 0:1e-4:5/F;        % TEST axis
xc = cos(2*pi*F*tc);    % CT signal
td = 0:Ts:5/F;          % DT axis
xd = cos(2*pi*F*td);    % DT signal
N = length(td);         % number of samples

%% Reconstruction at using the formula:
% xr(t) = sum over n=0,...,N-1: x(nT)*sin(pi*(t-nT)/T)/(pi*(t-nT)/T)
% Note that sin(pi*(t-nT)/T)/(pi*(t-nT)/T) = sinc((t-nT)/T)
% sinc(x) = sin(pi*x)/(pi*x) according to MATLAB
xr = zeros(size(tc)); %initialization
sinc_train = zeros(N,length(tc)); %initialization
for n = 0:N-1
   %unless we define our sinc with a value in zero computers will introduce Nu which   %lead to a small error   %sinc_train(n+1,:) = sin(pi*(tc-n*Ts)/Ts)./(pi*(tc-n*Ts)/Ts); %sinc train   sinc_train(n+1,:) = sinc((tc-n*Ts)/Ts); %sinc train   current_sinc=xd(n+1)*sinc_train(n+1,:); %a sinc scaled by the sample value   xr = xr + current_sinc; %generation of the recovered signal summing the sinc scaled
end

%% Plot the results
figure
hold on
grid on
plot(tc,xc,'b','linewidth',2)
stem(td,xd,'k','linewidth',2)
plot(tc,xr,'r','linewidth',2)
legend('Continuos Signal','Sampled Signal','Reconstructed Signal')
xlabel('Time [sec]')
ylabel('Amplitude')

%% Sinc train visualization    
figure

%all at once display
%hold on
%grid on
%plot(tc,xd'.*sinc_train)
%plot(tc,xr,'r','linewidth',2)
%stem(td,xd)

%progress display
xr_progress=zeros(size(tc)); %initialization
for n = 0:N-1
  clf;hold on;grid on;  current_sinc=xd(n+1)*sinc_train(n+1,:);
  stem(td(1:n+1),xd(1:n+1),'k','linewidth',2)
  plot(tc,xd(1:n+1)'.*sinc_train(1:n+1,:))
  xr_progress=xr_progress+current_sinc;
  plot(tc,xr_progress,'r','linewidth',2)
  xlabel('Time [sec]')
  ylabel('Amplitude')
  title(['Step ',num2str(n+1),' (Having ',num2str(n+1),' Sincs)'])
  sleep(5)
end
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to is terms of service and acknowledge thou must readers our privacy policy.

Not the answer you're looking for? Browse other questions tagged either ask their own question.