frequency error at fft of cos
Show older comments
Hi I'm new to matlab.
I'm making some filter but while making it I found some error at freq..
Here is my sample code for transforming a cosine function with frequency f_c.
clear
Dur=10;
Fs=44100*2;
t1 = 0:1/(Fs):Dur;
t1=t1(1:end-1);
f_c=1000;
feq = -(2*f_c+Fs/2):(4*f_c+Fs)/((Fs*Dur)-1):(2*f_c+Fs/2)
%can't change up to this point.
func=cos(2*pi*f_c*t1)
FUNC=fftshift(fft(func))
plot(feq, FUNC)
What I expected was two delta func at f=f_c, -f_c (in this case 1000, -1000).
but I got delta func at f=1045, -1045 (approx).
Why do I get this error?? And how can I eliminate the error?
I have larger f_c to calculate(ex. f_c=20000), so I get much larger error...
2 Comments
KALYAN ACHARJYA
on 1 Dec 2018
-but I got delta func at f=1045, -1045 (approx).
Can you show the figure along with pointing 1045 and -1045?
JINHO CHOI
on 1 Dec 2018
Answers (1)
David Goodmanson
on 2 Dec 2018
Edited: David Goodmanson
on 2 Dec 2018
Hi JC,
I will not attempt to figure out what went wrong, but try
n = length(t1);
feq = (-n/2:n/2-1)*(Fs/n);
also, dividing by n in
FUNC=fftshift(fft(func)/n);
gives the correct amplitudes for the peaks, 1/2 for each one.
Categories
Find more on Transforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!