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');                          

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

fs1=2*fm + 10
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. This is my signal

This is what i want to do

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

  • $\begingroup$ 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/…. $\endgroup$ Commented Apr 18, 2020 at 3:27
  • $\begingroup$ I didnt share my all code, t is defined actually. I am asking how can i make sampling on this signal. $\endgroup$ Commented Apr 18, 2020 at 11:56
  • 1
    $\begingroup$ OK, but t is "everything". I've supplied an answer that I think is what you're after. $\endgroup$ Commented Apr 18, 2020 at 19:06

2 Answers 2


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:

enter image description here

  • $\begingroup$ This is useful for me but I have one more question. At the nyquist rate, why sampling points are not on the peaks $\endgroup$ Commented Apr 18, 2020 at 19:44
  • $\begingroup$ 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. $\endgroup$ Commented Apr 19, 2020 at 1:20

You may be interested in the simple experiment using matlab. https://poweidsplearningpath.blogspot.com/2020/04/ch4-adcdac-how-to-simulate-adcdac.html

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.

enter image description here

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);

% ADC: Quantizer
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);

% add a small high frequency component as asked.       
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;

%% ADC
% 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
% be added here.
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

hold on;
title('analog signal (1500Hz) v.s. digital signal (5Hz) v.s. Reconstructed signal (1500Hz)');


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).

enter image description here

  1. A. Oppenheim and R. Schafer, Discrete Time Signal Processing 3rd. 2009 As others have said, the book 1 is called the DSP bible.
  • $\begingroup$ 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 ? $\endgroup$ Commented Apr 18, 2020 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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