1
$\begingroup$

I have a sinusoidal signal namely:

signal = sin(2*pi*0.08*t) + sin(2*pi*0.2*t) + sin(2*pi*0.32*t) + sin(2*pi*0.4*t);

so the segment of the code becomes:

freqs = [0.08, 0.2, 0.32, 0.4];
periods = 1./ freqs;
% Let the max value of t be 4 times the largest period
t_max = 4 * periods(1);
% Let us have 500 samples over the interval [0, t_max].
t = linspace(0, t_max, 500);
% The synthesized signal 
signal = sin(2*pi*0.08*t) + sin(2*pi*0.2*t) + sin(2*pi*0.32*t) + sin(2*pi*0.4*t);

Now I wanna view the amplitude spectrum for the first 50 samples.

So I did this:

signal_first_50_samples= signal (1:50);
% Display its magnitude spectrum. 
N = 64;
signal_spect = abs (fft(signal_first_50_samples,N));
signal_spect = fftshift(signal_spect);
F = [-N/2:N/2-1]/N;
plot (F, signal_spect) 
set(gca, 'XTick',[-0.5:0.1:1])
grid on;

I was expecting spikes at each of the frequencies, however, I got this: enter image description here

I re-did the sampling part as follows, to include more samples:

%% Synthesizing a sampled signal that consists of 4 sine waves 
% We are given 4 different frequencies 
freqs = [0.08, 0.2, 0.32, 0.4];
periods = 1./ freqs;
% Let the max value of t be 4 times the largest period
t_max = 4 * periods(1);
% Let us have 500 samples over the interval [0, t_max].
t = linspace(0, t_max, 500);
% The synthesized signal 
signal = sin(2*pi*0.08*t) + sin(2*pi*0.2*t) + sin(2*pi*0.32*t) + sin(2*pi*0.4*t);
% Visualising the synthesized signal 
plot(t, signal, 'b-', 'LineWidth', 1.5);
grid on;
title('The Synthesized Signal', 'FontSize', fontSize);
xlabel('t', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Display its magnitude spectrum. 
% i was expecting spikes at each freq 
N = 64;
signal_spect = abs (fft(signal,N));
signal_spect = fftshift(signal_spect);
F = [-N/2:N/2-1]/N;
plot (F, signal_spect) 
%set(gca, 'XTick',[-0.5:0.05:1])
grid on;

but i still got approx the same result, not what i expected enter image description here

another edit: i did something else i had only 50 samples for the singal and took all of them

%% 50 samples taking 50
clc;    
clear;  
% We are given 4 different frequencies 
freqs = [0.08, 0.2, 0.32, 0.4];
periods = 1./ freqs;
% Let the max value of t be 4 times the largest period
t_max = 4 * periods(1);
% Let us have 50 samples over the interval [0, t_max].
t = linspace(0, t_max, 50);
% The synthesized signal 
signal = sin(2*pi*0.08*t) + sin(2*pi*0.2*t) + sin(2*pi*0.32*t) + sin(2*pi*0.4*t);
% Determine the first 50 samples of this signal 
% signal_first_50_samples= signal (1:50);
% Display its magnitude spectrum. 
% ??????????????????????????????????????????????????????????????????????
% I was expecting spikes at each freq 
N = 64;
signal_spect = abs (fft(signal,N));
signal_spect = fftshift(signal_spect);
F = [-N/2:N/2-1]/N;
figure
plot (F, signal_spect) 
%set(gca, 'XTick',[-0.5:0.05:1])
grid on;

I got this enter image description here

which looks about right but I wonder why it didn't work when i had 500 samples and i took all 500 of them

any ideas?

$\endgroup$
  • $\begingroup$ are you sure you are sampling it right? $\endgroup$ – D.Zou Dec 7 '14 at 5:43
  • $\begingroup$ I thought I undersampled the signal so i re-did the sampling as in the edit, but its still the same $\endgroup$ – HappyBee Dec 7 '14 at 6:59
  • $\begingroup$ any thoughts on the new edits @D.Zou $\endgroup$ – HappyBee Dec 7 '14 at 7:45
  • $\begingroup$ honestly I didn't read through the entire post... I saw the equations and the plot, the only way you could get a plot like that is if you undersampled it so that was my chief suspect. I will have to re-read the whole thing and get back to you. $\endgroup$ – D.Zou Dec 7 '14 at 8:42
0
$\begingroup$

A few suggestions and comments:

  • You calculate the frequency vector with F = [-N/2:N/2-1]/N;. This is wrong. You need to take the sampling frequency into account.

  • The way you organized your code, you don't have direct control over the sampling frequency. I recommend specifying both the time length and the sampling frequency explicitly in your code.

  • You need to set up your analysis so that you get high resolution in the frequency domain (so that you can resolve peaks that are very near to each other). The only way to achieve this is to sample for a long time. The more you oversample the signal (that is, the higher the Nyquist frequency is), the longer time you need to observe the signal. This is the reason you want to have explicit control over sampling rate and signal duration.

  • Do not limit the FFT size by choosing an explicit N, since that may lower your frequency resolution. If you want to set the FFT to a power of 2, then use nextpow2 or similar.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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