講解:DFT括改、Matlab腻豌、Matlab、FFTSQL|Matlab

IntroductionThis lab is a revision of the Discrete Fourier Transform (DFT), and theFast Fourier Transform (FFT), and an introduction to the Short-Time Fourier Transform (STFT) and thespectrogram.The outcomes from the lab are to be handed in as a “folder” of results, showing thatyou have completed the steps of the lab successfully. A box in the right-handmargin indicates where an outcome is expected – like this:The Matlab programs you write will be short: you can print them out if you wish, buthand-written listings are OK too. Sketch graphs are also OK.1. Getting StartedIn your home directory, create the subdirectories “EBU6018” and “EBU6018/lab1”.Start Matlab. Use “cd ” to get into the directory “l(fā)ab1” you have justcreated.2. Discrete Fourier TransformIn Matlab, type “edit” to start the Matlab editor.Create a Matlab function in the file “dft.m” to calculate the Discrete FourierTransform of a signal. Recall that the DFT is given by [Qian, eqn (2.34)][NB: The “j” is missing in Qian’s definition of WN on p33.]Hints:? Start your function with function sw = dft(st)so “st” is the time waveform vector, and “sw” is the frequency waveform vector? Matlab vectors (e.g. st and sw) start from 1, not zero, so use “n-1” and “m-1” torefer to the appropriate element? Assume that N=M, and use the “l(fā)ength(st)” to find the value to use for these.An example outline for your Matlab function is provided below.EBU6018 Advanced Transform MethodsLab 1: DFT, FFT and STFTDepartment of Electronic Engineering and Computer ScienceExample DFT function outline in MatlabGenerate some waveforms to test your function. Test your dft on at least thefollowing signals:? Uniform function: “s=ones(1,64);”? Delta function: “s = ((1:64)= =1);”[NB: “1:64” generates the vector (1 2 … 64) ].? Sine wave: “s = sin(((1:64)-1)*2*pi*w/100)” for various values of w.Why do we need to use “(1:64)-1”?What values of w give the cleanest dft?What happens if we use “cos”?? Symmetrical rectangular pulse: “s = [0:31 32:-1:1](NB: Why doesn’t this “l(fā)ook” symmetrical? Remember that the DFT repeats, sothe time interval 32 .. 63 is “the same as” the interval -31 .. -1).The following function may be useful to display your results:If you want zero frequency (or time) to appear in the middle of your plot, use“fftshift”, e.g. “stem4(fftshift(dft(s)));”Explain your results in terms of what you know about the Fourier Transform.function stem4(s)% STEM4 - View complex signal as real, imag, abs and anglesubplot(4,1,1); stem(real(s)); title(Real);subplot(4,1,2); stem(imag(s)); title(Imag);subplot(4,1,3); stem(abs(s)); title(Abs);subplot(4,1,4); stem(angle(s)); title(Angle);endfunction sw = dft(st)% DFT - Discrete Fourier TransformM = length(st);N = M;WN = exp(2*pi*j/N);% Main loopfor n=0:N-1 temp = 0; for m=0:M-1 [** Do something useful here **] end sw(n+1) = temp;end 3. Comparison with Matlab’s FFT functionMatlab has a built-in Fast Fourier Transform, “fft”.Compare the results of your dft against the built-in fft. Are the results the same? Ifso, why: if not, why not?Find out the complexity of your dft and the built-in fft, i.e. how long they take toperform their calculation for various lengths of s. Use “tic” and “toc” to measure thetime taken to perform the operation, so e.g. tic; dft(ones(1,4)); toc % No “;” for final expressionwill report how long a 4-point DFT took to calculate.Hint: You may find your dft is too fast for tic/toc to measure any useful difference. Ifso, run it several times, e.g.tic; for (i=1:1e4) dft(ones(1,4)); end; toc(Of course, remember to divide your measure by the number of times round the loop!)Make a log-log plot (using “l(fā)oglog”) showing the time increase with the size n of s.On your plot, show that the DFT takes O(n2) time, while the FFT takes O(n log n).Hint: Use “hold on” if you want to add a second “l(fā)oglog” plot to an existing plot.Explain what this tells you about the DFT compared to the FFT in real applications,i.e. as n gets larger.*[OMIT]3.1 DIY-FFT [Optional, but highly recommended] [OMIT]Write a Matlab function (called e.g. “my_fft”) to calculate the FFT of a signal. Ifyou like, you could write this as a recursive function (one that calls itself) – see theoutline below.Plot and compare its speed to the DFT, showing that your “my_fft” function takesO(n log n) time rather than O(n2) timeDerivation of the FFT:odd the of FFT point- the is and the is where samples, even the ofFFT point:get weodd for and even for usingeven oddNotes:(1) The above only works if N is a power of 2 (64, 128, 1024, etc), so your programmay not work if you use other lengths of s (you could check this, if you like!)(2) Note that a 1-point FFT of a signal is the signal itself, so the 1-point FFT is easy(to be sure of this, check the DFT formula with N=1).(3) Remember that Matlab vectors start at 1 (not zero), so go from 1...N not 0…N-1Example outline of Matlab function to calculate FFT:function sw = my_fft(st);% Recursive Implementation of Fast Fourer TransformN = length(st);% check length of N is 2^kif (rem(log(N),log(2))) disp(slow_fft: N must be an exact power of 2) returnendWN = exp(2*pi*j/N);% split st into even and odd samplesst_even = st(1:2:end-1);st_odd = st(2:2:end);% implement recursion here...if (N==2) g = st_even; % = st(0+1) h = st_odd; % = st(2) gg = [g g]; hh = [h -h];else g = [** Something useful here **]; h = [** Something useful here **]; gg = [g g]; hh = WN.^(-[0:N-1]).*[h h];endsw = gg+hh; 4. Single Windowed Fourier TransformSave one of the audio files on the course details page athttps://www.student.elec.qmul.ac.uk/courseinfo/EBU6018/into your “l(fā)ab1” directory.Read into Matlab, using “s = wavread(file.wav)”.Where ‘file.wav’ could be ‘dbarrett2.wav’Plot the magnitude (“abs”) of the FFT of the waveform. (“plot” is probably betterthan “stem” for these longer signals). Explain what this tells you about the waveform.We will now construct a function that will allow you to “zoom in” on a short sectionof the signal. To smooth out end effects, we will use a “Hanning” window to multiplythe segment that we select. You can show the Hanning window of length 256 inMatlab using “plot(hanning(256))”.Construct a Matlab function in the file “wft.m” that will select a section from a fileand window it. The function “wft” is to be called as follows: y = wft(s, t, n);where s is the signal, t is the time in the middle of the window, and n is a windowlength. You might use the following steps:1) Select the desired section from the signal, for example usings(floor(t-n/2)+(1:n)); (if you don’t see how this works, try “help colon”).2) Multiply elementwise with a Hanning window of length n, using “.*”3) Use the built-in Matlab fft function to calculate the DFT.Plot the magnitude of this single windowed Fourier transform of your signal forvarious values of t and n (note that values of t near the beginning and end of s maycause an error, depending on how clever you were at step (1)). Try also plotting with alog y-scale. Explain the difference between these results*[OMIT][Optional]: Make a matlab m-file that loops through different values for t in steps ofe.g. 50, using “pause” between each step.5. STFT and SpectrogramNow we will construct a “spectrogram” to visualize the time-frequency information ina signal on one image.Read the Matlab documentation for the Matlab “specgram” function (try “helpspecgram” for information).Using specgram, investigate the audio files on the course details page athttps://www.student.elec.qmul.ac.uk/courseinfo/EBU6018/Try different window sizes (“NFFT”) to see the effect. For fastest results on longfiles, use powers of 2 (Why?). Record what values of window size give bestvisualization results for different files, and suggest why. 5.1 Analysis of Piccolo soundFrom the course webpage download ‘piccolo.wav’ and load it into Matlab using:[x fs] = wavread(‘piccolo.wav’); % fs = sampling frequencyRecord the sampling frequency, fs.If you have headphones, try listening to the signal, usingsoundsc(x,fs); %fs is the sampling frequency of xPlot a spectrogram of x, using the ‘specgram’ function.From the spectram plot, estimate the fundamental frequencies (f0) of the 3 notes inthe sample, giving your answers in Hz.Repeat your estimates for different window sizes.Notes: You will need to use your window size, (NFFT) and the value for the samplingfrequency (fs) in your calculation. Figure 1 is given as a guide to help you.Make your calculation in 2 ways:(1) by calculating the frequency range displayed by specgram, and(2) by supplying specgram with the correct value fs when you call it.Check that both of these methods agree.Figure 1: Angular frequency representation for f0 estimationExplain what happens to the accuracy of your f0 values as you vary the window size.For further experimentation, try visualizing other “wav” files available on the internetusing your spectrogram. *[OMIT]5.1 DIY STFT and Spectrogram [Optional, but highlyrecommended]Construct a Matlab function “sg(s,N)” in a file called “sg.m” to compute aspectrogram of a waveform s with window size N (NFFT in Matlab’s specgram).To do this, your function shouldi) divide the signal “s” into sections of length N,ii) multiply s by a Hanning windowiii) perform an FFT of each section, andiv) construct a matrix where each column is the absolute value of one FFTHints:? You can select the k-th segment of length N using “s( ((1:N)+(k-1)*N) )”? You can get a Hanning window of length N by using the Matlab function“W=hanning(N)”. Multiply by a segment s1 using “s1.*W” (dot-star).? Since the signal is real, you know the FFT result will be Hermitian symmetric, soyou can discard one half of the vector of results.? You can set the n-th column of a matrix to be a 1xN vector y by usingM(:,n) = yPlot usingimagesc(log10(abs(B))); axis xy;where B is the spectrogram (“axis xy” restores the origin to the bottom).How should you call “specgram” to get the most similar results to your function “sg”?Modify your function “sg” so that it overlaps its windows in the same way as thedefault operation of “specgram”.6. Handing InCompile the answers to the exercises, including the answers to specific questions,program listings (including comments), and plots from experiments, into a “folder” ofresults showing that you have completed the lab, and submit electronically. You donot need to write a formal report.IMPORTANT: Plagiarism (copying from other students, or copying the work ofothers without proper referencing) is cheating, and will not be tolerated.IF TWO “FOLDERS” ARE FOUND TO CONTAIN IDENTICAL MATERIAL,BOTH WILL BE GIVEN A MARK OF ZERO.Updated by MPD, MEPDModified ARW for EBU6018.轉(zhuǎn)自:http://www.daixie0.com/contents/12/4346.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嘱能,一起剝皮案震驚了整個濱河市吝梅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌惹骂,老刑警劉巖苏携,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異对粪,居然都是意外死亡右冻,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門著拭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纱扭,“玉大人,你說我怎么就攤上這事儡遮∪槎辏” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長屡久。 經(jīng)常有香客問我忆首,道長,這世上最難降的妖魔是什么被环? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮详幽,結(jié)果婚禮上筛欢,老公的妹妹穿的比我還像新娘。我一直安慰自己唇聘,他們只是感情好版姑,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著迟郎,像睡著了一般剥险。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上宪肖,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天表制,我揣著相機(jī)與錄音,去河邊找鬼控乾。 笑死么介,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蜕衡。 我是一名探鬼主播壤短,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼慨仿!你這毒婦竟也來了久脯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤镰吆,失蹤者是張志新(化名)和其女友劉穎帘撰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鼎姊,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡骡和,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了相寇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片慰于。...
    茶點(diǎn)故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖唤衫,靈堂內(nèi)的尸體忽然破棺而出婆赠,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布休里,位于F島的核電站蛆挫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏妙黍。R本人自食惡果不足惜悴侵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望拭嫁。 院中可真熱鬧可免,春花似錦、人聲如沸做粤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怕品。三九已至妇垢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肉康,已是汗流浹背闯估。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留迎罗,地道東北人睬愤。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像纹安,于是被迫代替她去往敵國和親尤辱。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容