How to correctly make FFT of sound set?

During 7 sec 7 tones plays with frequences (all in Hz), one tone -- one sec. Code:
Fs=44100;f=[261.63 293.67 329.63 349.23 392.00 440 493.88];
duration=7;octava=2;f=f/octava;Df=duration*Fs;FF=2*pi/Fs*f;
len = length(f);tau=Df/len;
n=1:Df;y=zeros(1,Df);
for i = 1:len
amplitude=(n>(i-1)*tau)&(n<i*tau);
y = y + sin(FF(i)*n).*amplitude;
end
soundsc(y, Fs);
This question rather for mathematicians. Now I have to see Fourier tranformation. Perhaps it will not accurate vertical Dirac bars but what?
Code:
Y1=fft(y);plot(abs(Y1))
Two bar on edges? How to make beauty picture? And what frequencies will be?

 채택된 답변

Rick Rosson
Rick Rosson 2011년 6월 17일

0 개 추천

Maybe this will help:
%%Parameters:
playAudio = false;
%%Time domain:
Fs = 44100;
dt = 1/Fs;
StartTime = 0;
StopTime = 1;
t = (StartTime:dt:StopTime-dt)';
%%Cosine waves:
Fc = [261.63 293.67 329.63 349.23 392.00 440 493.88];
y = cos(2*pi*t*Fc);
N = size(y,2);
%%Reshape signal to a single column vector:
y = y(:);
%%Reformulate time domain:
StopTime = N*StopTime;
t = (StartTime:dt:StopTime-dt)';
M = size(t,1);
%%Frequency domain:
dF = Fs/M;
f = -Fs/2:dF:Fs/2-dF;
Y = (N/M)*fftshift(fft(y));
%%Play the music:
if playAudio
sound(y,Fs);
end
%%Plot time domain:
figure;
plot(t,y);
%%Plot frequency domain:
figure;
plot(f,abs(Y));
HTH.
Rick

댓글 수: 4

Igor
Igor 2011년 6월 17일
The best -- y=y(:) :-)
At zoom on figure I see 7 peaks symmetrically..
And I have to understand this result, and I shell push "Accept"-button!
Rick Rosson
Rick Rosson 2011년 6월 17일
If you have any questions about this code or why it works, please let me know.
Rick Rosson
Rick Rosson 2011년 6월 17일
Also, there are a few things about this script that are not quite optimal. Can you figure out what they are and how to fix them?
Danilo Persico
Danilo Persico 2021년 7월 9일
Hello Mr. Rosson, I would like if you could give an explaination of the code, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Daniel Shub
Daniel Shub 2011년 6월 15일

0 개 추천

The fft assumes that the signal is cyclical. You need to match up your edges (or zero pad) to get delta functions. The fft also returns both positive and negative frequencies. I would read the documentation about fft and fftshift.
Rick Rosson
Rick Rosson 2011년 6월 16일

0 개 추천

Please try the following:
dt = 1/Fs;
len = duration*Fs;
n = dt*(0:len-1);
Df = Fs/len;
f = -Fs/2:Df:Fs/2-Df;
Also:
Y1 = fftshift(fft(y));
plot(abs(Y1));
HTH.

댓글 수: 1

Igor
Igor 2011년 6월 17일
Not clear...
1st part -- replace my 2nd row? but what is the sense of *f*?
how does the 2nd part refer to 1st part?
(because of, y-array is unchanged data)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Measurements and Spatial Audio에 대해 자세히 알아보기

질문:

2011년 6월 14일

댓글:

2021년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by