Why is frequency of graph differing?

1 view (last 30 days)
I like to graph the frequency of 440 Hz and analyse it
I use the following code:
figure(1)
sf = 1000; %sampling
f = 440;
a = 0.5;
t = 0:1/sf:1;
x = a*sin(2*pi*f*t);
sound(x,sf)
figure(1)
plot(t,x) %amplitude-time graph
axis([0 1/f min(x) max(x)])
figure(2) %Fourier analysis
n = length(t);
xhat = fft(x,n);
PSD = xhat.*conj(xhat)/n;
freq = 1/(dt*n)*(0:n);
L = 1:floor(n/2);
colormap jet(20)
plot(freq(L),PSD(L),'LineWidth',2.5)
First of: why is it, that the Frequency of amp-time Graph is jagged? If I increase the sampling frequency, why is, that the actual frequency changing?
  6 Comments
Niklas Kurz
Niklas Kurz on 18 Jan 2021
can you post this as answere pls so I can accept .

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 19 Jan 2021
hi - official answer this time
the PSD concept is valid only for random type signals , not sinus
mathematically speaking, a sinus has a PSD of infinite amplitude and zero bandwith, simply because a sine wave has its energy concentrated only at one discrete frequency and not spread accross a wider freq range
if you test your code with random signals, you should see that the peak amplitude stays the same whatever the sampling frequency. But this does not hold if you test now with a sine wave
for a sine wave , the psd concept must be replaced by power or rms or peak amplitude
in summary , a FFT analyser will operate this way :
it will first compute the ftt.*conj(fft) power spectrum
if you ask for "PSD" (knowing that you are looking at random type signals) , it will do : PSD = power / nfft (as you did)
if you ask for "RMS" (knowing that you are looking at sine / multi sine type signals) , it will do : RMS = sqrt(power) and there is no division by nfft
so I modified a bit your code to test the different scenarios :
sf = 2200; %sampling
f = 440;
a = 0.5;
t = 0:1/sf:1;
% x = a*sin(2*pi*f*t); % to test the PSD = 2*abs(xhat)/n
x = a*randn(size(t)); % to test the PSD = xhat.*conj(xhat)/n
sound(x,sf)
figure(1)
plot(t,x) %amplitude-time graph
axis([0 1/f min(x) max(x)])
figure(2) %Fourier analysis
n = sf; % so df = sf/n = 1 Hz
xhat = fft(x,n);
PSD = xhat.*conj(xhat)/n; % ok for random type signals
% PSD = 2*abs(xhat)/n; % ok for sine type signals
freq = 1/(1/sf*n)*(0:n);
L = 1:floor(n/2);
colormap jet(20)
plot(freq(L),PSD(L),'LineWidth',2.5)

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!