annotate extra/harmonicmodel1.m @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 function [y]=harmonicmodel1(x,w,N,t)
yading@10 2 %initializing values
yading@10 3 M = length(w); % window size - the longer the more frequency resolution
yading@10 4 N2 = N/2+1; % positive part of the spectrum
yading@10 5 Ns= 1024; % FFT size for synthesis (even)
yading@10 6 H = 256; % analysis/synthesishop size
yading@10 7 soundlength = length(x); % length of input sound array - samples
yading@10 8
yading@10 9 fftbuffer = zeros(N,1); % initialize buffer for FFT
yading@10 10
yading@10 11 %Create a loop to step through the sound array x
yading@10 12 %initializing the loop
yading@10 13 hNs = Ns/2; % half synthesis window size
yading@10 14 hM = (M-1)/2; % half analysis window size used to overlap windows
yading@10 15
yading@10 16 pin = max(hNs+1,1+hM); % initialize sound pointer to middle of analysis window
yading@10 17 pend = soundlength-max(H,hM); % last sample to start a frame
yading@10 18
yading@10 19 y = zeros(soundlength,1); % initialize output array
yading@10 20 w = w/sum(w); % normalize analysis window
yading@10 21 sw = zeros(Ns,1);
yading@10 22 ow = triang(2*H-1); % overlapping window
yading@10 23 ovidx = Ns/2+1-hNs+1:Ns/2+H; % overlap indexes
yading@10 24 sw(ovidx) = ow(1:2*H-1);
yading@10 25 bh = blackmanharris(Ns); % synthesis window
yading@10 26 bh = bh ./ sum(bh); % normalize synthesis window
yading@10 27 sw(ovidx) = sw(ovidx) ./ bh(ovidx);
yading@10 28
yading@10 29 while pin<pend
yading@10 30 xw = x(pin-hM:pin+hM).*w(1:M)'; % window the input sound - STFT definition
yading@10 31
yading@10 32 %zero phased window
yading@10 33 fftbuffer(:) = 0; % reset buffer
yading@10 34 fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase fftbuffer
yading@10 35 fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
yading@10 36
yading@10 37 %compute FFT of the zero phased frame
yading@10 38 X = fft(fftbuffer);
yading@10 39
yading@10 40 %calculate magnitude and phase spectrum of of positive frequencies
yading@10 41 mX = 20*log10(abs(X(1:N2)));
yading@10 42 pX = unwrap(angle(X(1:N2)));
yading@10 43
yading@10 44
yading@10 45 %Find the locations, ploc, of the local maxima above a given
yading@10 46 %threshold, t, in each magnitude spectrum by finding changes of slope.
yading@10 47 ploc = 1+find((mX(2:N2-1)>t).*(mX(2:N2-1)>mX(3:N2)).*(mX(2:N2-1)>mX(1:N2-2)));
yading@10 48
yading@10 49 %Find the magnitudes, pmag, and phases, pphase, of the obtained
yading@10 50 %locations.
yading@10 51 pmag = mX(ploc);
yading@10 52 %pmag = mX(ploc)*0.4;
yading@10 53 pphase = pX(ploc);
yading@10 54
yading@10 55
yading@10 56 %peak interpolation
yading@10 57 [iploc, ipmag, ipphase] = peakinterp (mX, pX, ploc);
yading@10 58
yading@10 59
yading@10 60 %plot for a window
yading@10 61 if (pin==1+hM+20*H)
yading@10 62 figure
yading@10 63 subplot(2,1,1)
yading@10 64 plot(mX)
yading@10 65 hold on
yading@10 66 plot(iploc,mX(ploc),'*')
yading@10 67 title('magnitude and peak values');
yading@10 68 hold off
yading@10 69 subplot(2,1,2)
yading@10 70 plot(pX)
yading@10 71 hold on
yading@10 72 plot(iploc,pX(ploc),'*')
yading@10 73 title('phase and peak values');
yading@10 74 hold off
yading@10 75
yading@10 76 %number of peaks
yading@10 77 npeaksm = length(pmag)
yading@10 78 npeaksp = length(pphase)
yading@10 79 end
yading@10 80
yading@10 81
yading@10 82 %-----synthesis-----%
yading@10 83 plocs = (ploc-1)*Ns/N+1; % adapt peak locations to synthesis FFT
yading@10 84 Y = genspecsines(plocs,pmag,pphase,Ns); % generate spec sines
yading@10 85 yw = fftshift(real(ifft(Y))); % time domain of sinusoids
yading@10 86 y(pin-hNs:pin+hNs-1) = y(pin-hNs:pin+hNs-1)+ sw.*yw(1:Ns); % overlap-add
yading@10 87 pin = pin+H; % advance the sound pointer
yading@10 88
yading@10 89 end
yading@10 90 end