yading@10
|
1 function y = stftlab4(x, w, N, H, t)
|
yading@10
|
2 % Analysis/synthesis of a sound using the peaks
|
yading@10
|
3 % of the short-time fourier transform
|
yading@10
|
4 % x: input sound, w: analysis window (odd size), N: FFT size, H: hop size,
|
yading@10
|
5 % t: threshold in negative dB, y: output sound
|
yading@10
|
6 M = length(w); % analysis window size
|
yading@10
|
7 N2 = N/2+1; % size of positive spectrum
|
yading@10
|
8 soundlength = length(x); % length of input sound array
|
yading@10
|
9 hM = (M-1)/2; % half analysis window size
|
yading@10
|
10 pin = 1+hM; % initialize sound pointer at the middle of analysis window
|
yading@10
|
11 pend = soundlength-hM; % last sample to start a frame
|
yading@10
|
12 fftbuffer = zeros(N,1); % initialize buffer for FFT
|
yading@10
|
13 yw = zeros(M,1); % initialize output sound frame
|
yading@10
|
14 y = zeros(soundlength,1); % initialize output array
|
yading@10
|
15 w = w/sum(w); % normalize analysis window
|
yading@10
|
16 sw = hanning(M); % synthesis window
|
yading@10
|
17 sw = sw./sum(sw);
|
yading@10
|
18 while pin<pend
|
yading@10
|
19 %-----analysis-----%
|
yading@10
|
20 xw = x(pin-hM:pin+hM).*w(1:M)'; % window the input sound
|
yading@10
|
21 fftbuffer(:) = 0; % reset buffer
|
yading@10
|
22 fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase fftbuffer
|
yading@10
|
23 fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
|
yading@10
|
24 X = fft(fftbuffer); % compute the FFT
|
yading@10
|
25 mX = 20*log10(abs(X(1:N2))); % magnitude spectrum of positive frequencies
|
yading@10
|
26 pX = unwrap(angle(X(1:N2))); % unwrapped phase spectrum
|
yading@10
|
27 ploc = 1+find((mX(2:N2-1)>t).*(mX(2:N2-1)>mX(3:N2)).*(mX(2:N2-1)>mX(1:N2-2))); % peaks
|
yading@10
|
28 [nploc,npmag,npphase] = peakinterp(mX,pX,ploc); % refine peak values. i use new parameters
|
yading@10
|
29 %so that i can plot the new values at the figures existed
|
yading@10
|
30 pmag = mX(ploc); % finding the magnitude of the obtained locations (peaks)
|
yading@10
|
31 pphase = pX(ploc); % finding the phase of the obtained locations (peaks)
|
yading@10
|
32
|
yading@10
|
33 % plotting the peak values on top of the magnitude and phase spectra at the
|
yading@10
|
34 % magnitude and phase frame
|
yading@10
|
35
|
yading@10
|
36 subplot(211);
|
yading@10
|
37 plot(mX);
|
yading@10
|
38 hold on
|
yading@10
|
39 plot(ploc,pmag,'m*'); %m is for magenta color
|
yading@10
|
40 plot(nploc,npmag,'c*'); %plot the new magnitude spectra
|
yading@10
|
41 title('magnitude and peak values');
|
yading@10
|
42 hold off
|
yading@10
|
43 subplot(212);
|
yading@10
|
44 plot(pX);
|
yading@10
|
45 hold on
|
yading@10
|
46 plot(ploc,pphase,'m*');
|
yading@10
|
47 plot(nploc,npphase,'c*');
|
yading@10
|
48 title('phase and peak values');
|
yading@10
|
49 hold off
|
yading@10
|
50
|
yading@10
|
51 end |