annotate extra/harmonicmodel.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 = harmonicmodel(x, fs, w, N, t, nH, minf0, maxf0, f0et, maxhd)
yading@10 2 % Analysis/synthesis of a sound using the sinusoidal harmonic model
yading@10 3 % x: input sound, fs: sampling rate, w: analysis window (odd size),
yading@10 4 % N: FFT size (minimum 512), t: threshold in negative dB,
yading@10 5 % nH: maximum number of harmonics, minf0: minimum f0 frequency in Hz,
yading@10 6 % maxf0: maximim f0 frequency in Hz,
yading@10 7 % f0et: error threshold in the f0 detection (ex: 5), % maxhd: max. relative deviation in harmonic detection (ex: .2)
yading@10 8 % y: output sound
yading@10 9 M = length(w); % analysis window size
yading@10 10 Ns= 1024; % FFT size for synthesis
yading@10 11 H = 256; % hop size for analysis and synthesis
yading@10 12 N2 = N/2+1; % size postive spectrum
yading@10 13 soundlength = length(x); % length of input sound array
yading@10 14 hNs = Ns/2; % half synthesis window size
yading@10 15 hM = (M-1)/2; % half analysis window size
yading@10 16 pin = max(hNs+1,1+hM); % initialize sound pointer to middle of analysis window
yading@10 17 pend = soundlength-hM; % last sample to start a frame
yading@10 18 fftbuffer = zeros(N,1); % initialize buffer for FFT
yading@10 19 y = zeros(soundlength+Ns/2,1); % output sound
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-H+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 while pin<pend
yading@10 29 %-----analysis-----%
yading@10 30 xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound
yading@10 31 fftbuffer(:) = 0; % reset buffer
yading@10 32 fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase window in fftbuffer
yading@10 33 fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
yading@10 34 X = fft(fftbuffer); % compute the FFT
yading@10 35 mX = 20*log10(abs(X(1:N2))); % magnitude spectrum
yading@10 36 pX = unwrap(angle(X(1:N/2+1))); % unwrapped phase spectrum
yading@10 37 ploc = 1 + find((mX(2:N2-1)>t) .* (mX(2:N2-1)>mX(3:N2)) ...
yading@10 38 .* (mX(2:N2-1)>mX(1:N2-2))); % find peaks
yading@10 39 [ploc,pmag,pphase] = peakinterp(mX,pX,ploc); % refine peak values
yading@10 40 f0 = f0detection(mX,fs,ploc,pmag,f0et,minf0,maxf0); % find f0
yading@10 41 hloc = zeros(nH,1); % initialize harmonic locations
yading@10 42 hmag = zeros(nH,1)-100; % initialize harmonic magnitudes
yading@10 43 hphase = zeros(nH,1); % initialize harmonic phases
yading@10 44 hf = (f0>0).*(f0.*(1:nH)); % initialize harmonic frequencies
yading@10 45 hi = 1; % initialize harmonic index
yading@10 46 npeaks = length(ploc); % number of peaks found
yading@10 47 while (f0>0 && hi<=nH && hf(hi)<fs/2) % find harmonic peaks
yading@10 48 [dev,pei] = min(abs((ploc(1:npeaks)-1)/N*fs-hf(hi))); % closest peak
yading@10 49 if ((hi==1 || ~any(hloc(1:hi-1)==ploc(pei))) && dev<maxhd*hf(hi))
yading@10 50 hloc(hi) = ploc(pei); % harmonic locations
yading@10 51 hmag(hi) = pmag(pei); % harmonic magnitudes
yading@10 52 hphase(hi) = pphase(pei); % harmonic phases
yading@10 53 end
yading@10 54 hi = hi+1; %increase harmonic index
yading@10 55 end
yading@10 56 hloc(1:hi-1) = (hloc(1:hi-1)~=0).*((hloc(1:hi-1)-1)*Ns/N+1); % synth. locs
yading@10 57 %-----synthesis-----%
yading@10 58 Yh = genspecsines(hloc(1:hi-1),hmag,hphase,Ns); % generate sines
yading@10 59 yh = fftshift(real(ifft(Yh))); % sines in time domain
yading@10 60 y(pin-hNs:pin+hNs-1) = y(pin-hNs:pin+hNs-1) + sw.*yh(1:Ns); % overlap-add
yading@10 61 pin = pin+H; % advance the input sound pointer
yading@10 62 end