SHOW:
|
|
- or go back to the newest paste.
1 | - | function y = stft(x, w, N, H, fs) |
1 | + | function y = stft(x, w, N, H, fs,t) |
2 | % Analysis/synthesis of a sound using the short-time Fourier transform | |
3 | % Authors: J. Bonada, X. Serra, X. Amatriain, A. Loscos | |
4 | % x: input sound, w: analysis window (odd size), N: FFT size, H: hop size | |
5 | % y: output sound | |
6 | % | |
7 | %-------------------------------------------------------------------------- | |
8 | % This source code is provided without any warranties as published in | |
9 | % DAFX book 2nd edition, copyright Wiley & Sons 2011, available at | |
10 | % http://www.dafx.de. It may be used for educational purposes and not | |
11 | % for commercial applications without further permission. | |
12 | %-------------------------------------------------------------------------- | |
13 | ||
14 | M = length(w); % analysis window size | |
15 | N2 = N/2+1; % size of positive spectrum | |
16 | soundlength = length(x); % length of input sound array | |
17 | hM = (M-1)/2; % half analysis window size | |
18 | pin = 1+hM; % initialize sound pointer in middle of analysis window | |
19 | pend = soundlength-hM; % last sample to start a frame | |
20 | fftbuffer = zeros(N,1); % initialize buffer for FFT | |
21 | yw = zeros(M,1); % initialize output sound frame | |
22 | y = zeros(soundlength,1); % initialize output array | |
23 | w = w/sum(w); % normalize analysis window | |
24 | k = [0:1:N]; | |
25 | freq = fs*k/N; | |
26 | - | t = -80 ; % threshold |
26 | + | %t = -80 ; % threshold |
27 | ||
28 | %while pin<pend | |
29 | %-----analysis-----% | |
30 | clf; | |
31 | xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound | |
32 | plot(freq(1:M), abs(xw)); | |
33 | %pause | |
34 | fftbuffer(:) = 0; % reset buffer | |
35 | fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase window in fftbuffer | |
36 | fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2); | |
37 | X = fft(fftbuffer); % compute FFT | |
38 | mX = 20*log10(abs(X(1:N2))); % magnitude spectrum of positive frequencies | |
39 | ploc = 1 + find((mX(2:N2-1)>t).*(mX(2:N2-1)>mX(3:N2)).*(mX(2:N2-1)>mX(1:N2-2))); | |
40 | subplot(2,1,1); | |
41 | plot(freq(1:N2),mX); | |
42 | hold on; | |
43 | plot(freq(ploc),mX(ploc),'X'); | |
44 | hold off; | |
45 | pX = unwrap(angle(X(1:N2))); % unwrapped phase spect. of positive freq. | |
46 | ||
47 | subplot(2,1,2) | |
48 | plot(freq(1:N2),pX); | |
49 | hold on; | |
50 | plot(freq(ploc),pX(ploc),'X'); | |
51 | hold off; | |
52 | pmag=mX(ploc); | |
53 | pphase=pX(ploc); | |
54 | pmag; | |
55 | pphase; | |
56 | %-----synthesis-----% | |
57 | % Y = zeros(N,1); % initialize output spectrum | |
58 | % Y(1:N2) = 10.^(mX/20).*exp(i.*pX); % generate positive freq. | |
59 | % Y(N2+1:N) = 10.^(mX(N2-1:-1:2)/20).*exp(-i.*pX(N2-1:-1:2)); | |
60 | % % generate neg.freq. | |
61 | % fftbuffer = real(ifft(Y)); % inverse FFT | |
62 | % yw(1:(M-1)/2) = fftbuffer(N-(M-1)/2+1:N); % undo zero-phase window | |
63 | % yw((M+1)/2:M) = fftbuffer(1:(M+1)/2); | |
64 | % y(pin-hM:pin+hM) = y(pin-hM:pin+hM) + H*yw(1:M); % overlap-add | |
65 | % pin = pin+H; % advance sound pointer | |
66 | % end | |
67 | % wavwrite(y, fs,'out.wav'); | |
68 | - | end |
68 | + | end |
69 | ||
70 |