# Sampling and Reconstruction of digital signal in Matlab

I'm trying to write a program in Matlab that samples (using Nyquist theorem) and recovers signal. However, I cannot write sampling part for sum of 2 signal.

f1 = 30;
T1 = 1/f1;
amplitude1 = 1;
f2 = 60;
T2 = 1/f2;
amplitude2 = 1;
signal1 = amplitude1 * sin(2*pi*t/T1);
signal2 = amplitude2 * sin(2*pi*t/T2);
signal = signal1 + signal2;
plot(t, signal);
grid on;


I write this code.I want to draw Undersampling, sampling at Nyquist rate and oversampling. When make researching, I find code like that, but i cannot use it for my signal.

fm=input('Enter the Msg frequency(fm):\n');
xa1=amplitude*sin(2*pi*fm*t);
subplot(2,2,1);
plot(t,xa1);


What is fm in my signal ? How can i find it ?

fs1=2*fm + 10
n=0:1/fs1:1;
xa2=amplitude*sin(2*pi*n*fm);
subplot(2,2,2);
stem(n,xa2);
title('Above niquist rate');


I want to write a code like above for all 3 cases. And how can I find the Nyquist sampling rate for signal empirically ? Thank you all.

I know signals are not same. But I just want to do same thing on my signal.

• To start, for "plot(t, signal);" you haven't defined "t". I suppose you want to evaluate t at intervals, but that's not happening. Maybe you should look at another example of plotting a signal, like dsp.stackexchange.com/questions/66323/…. Apr 18 '20 at 3:27
• I didnt share my all code, t is defined actually. I am asking how can i make sampling on this signal. Apr 18 '20 at 11:56
• OK, but t is "everything". I've supplied an answer that I think is what you're after. Apr 18 '20 at 19:06

I've defined a sample rate, fs, in Hz to match your sine frequencies. The sin calculations would be in terms of normalized frequencies—f1/f2, for instance. Your variable t is the sample index, and I've defined numSamps as the number of samples to calculate.

fs = 130;       % sample rate
numSamps = 21;  % number of sample points to calculate

f1 = 30;
amplitude1 = 1;
f2 = 60;
amplitude2 = 1;

t = 0 : numSamps-1;

signal1 = amplitude1 * sin(2*pi*f1/fs*t);
signal2 = amplitude2 * sin(2*pi*f2/fs*t);
signal = signal1 + signal2;

plot(t, signal,'o');
grid on;

% overlay oversampled plot for reference
hold on;
t = 0 : .1 : numSamps-1;  % steps of 0.1 samples
signal1 = amplitude1 * sin(2*pi*f1/fs*t);
signal2 = amplitude2 * sin(2*pi*f2/fs*t);
signal = signal1 + signal2;
plot(t, signal);
hold off;


I've repeated the calculations at ten times the sample rate for a reference on the graph, obviously this could be done more elegantly.

Here's the resulting plot:

• This is useful for me but I have one more question. At the nyquist rate, why sampling points are not on the peaks Apr 18 '20 at 19:44
• At the Nyquist frequency, the points would depend on phase. If you started sampling at t=0 using sine, all sample points would be 0. If you did the same with a cosine, they'd alternate at the positive and negative peaks. Of course, we need to sample at more than twice the frequency of the highest component to satisfy the sampling theorem. Apr 19 '20 at 1:20

Reconstruction is essentially a kind of interpolation or so called digital to analog conversion (DAC). Detail descriptions are introduced in chapter 4.8.3 of the DSP Bible 1.

However, we all understand the ideal theory. But the problem is how to bridge the theory and practice. Because all the signals we can simulate in Matlab is essentially 'Digital'. So, the so-cal ADC/DAC is just an approaching simulation. Let me briefly justify the difference.

Ideal Reconstruction in DAC: There is typically a zero order hold circuit and reconstruction filter. Please note that the frequency response of zero order hold is not evenly flat so a reconstruction filter comes to an aid.

Matlab Simulation in DAC: There is no need to actually simulate a zero-order hold. All you need are a up sampling and an ideal low pass filter.

Because the t cannot be found in the question. I just post a AD DA practice sample code here. Hope it can help.

This sample code sample a simulated 2 Hz analog signal,x_a, (with sampling rate 1500 Hz) to a discrete time signal,x_d, with 5 Hz sampling rate (nearly Nyquist rate) and then reconstruct this back,x_r.

%% This code simulate the AD/DA processing discussed in Chapter 4.8.3 [1]
close all; clear all;

%% parameters.
% analog
analog_fps = 1500;
analog_window_time = 3; %sec
t = 0:1/analog_fps: analog_window_time-1/analog_fps;

% digital
digital_fps = 5;
n = downsample(t,analog_fps/digital_fps);

X_m = 1; % Range
B = 10;% Bit number.

%% Signal generation
freq_hz = 1; % Hz.
x_a_1 = 0.5*cos(2*pi*freq_hz*t+0.1);

signal_freq = 2; %Hz
x_a_2 = 0.5*cos(2*pi*signal_freq*t+pi/2);
x_a = x_a_1 + x_a_2;

% Sampling
x_s = downsample(x_a,analog_fps/digital_fps);

% Quantizing (abs of input value should not over 1)
% x_d = Quantizing(x_s,B,X_m);  % A For complete ADC, a quantizing should
x_d = x_s; % For your case, there is no quantizing here.

%% DAC
% up sample / DAC
x_up = upsample(x_d,analog_fps/digital_fps);

% LPF (Reconstruction Filters)
h = intfilt(analog_fps/digital_fps,4,0.9);
%% Important
% please not the parameter 0.9, ideally should be 1 for Nyquist rate.
% 0.9 here is ratio of Nyquist.
% Given known limit band signal, shourter ratio can enhance SNR by oversampling.
%  (i,e, here I filterout the freq larger than 2.5(Nyquist rate) * 0.9 = 2.25Hz)

x_r = filter(h,1,x_up);
x_r(1:floor(mean(grpdelay(h)))) = [];
x_r = [x_r zeros(1,floor(mean(grpdelay(h))))];

%% Display

figure;
plot(t,x_a);
hold on;
plot(n,x_d);
plot(t,x_r);
title('analog signal (1500Hz) v.s. digital signal (5Hz) v.s. Reconstructed signal (1500Hz)');
legend('x_a','x_d','x_r');


You can adjust the digital_fps with sampling rate higher than Nyquist rate (30 for example) and 0.9 in intfilt to 0.5 (shorter group delay).

1. A. Oppenheim and R. Schafer, Discrete Time Signal Processing 3rd. 2009 As others have said, the book 1 is called the DSP bible.
• No I just want to sampling on my signal in picture. I am adding one more picture what i want to do. Can u look it please ? Apr 18 '20 at 12:11