annotate code Kat/hpsmodel.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 881c3acf1164
children
rev   line source
katkost@1 1 function [y,yh,ys,fr0] = hpsmodel(x,fs,w,N,t,nH,minf0,maxf0,f0et,maxhd,stocf)
katkost@1 2 %=> analysis/synthesis of a sound using the sinusoidal harmonic model
katkost@1 3 % x: input sound, fs: sampling rate, w: analysis window (odd size),
katkost@1 4 % N: FFT size (minimum 512), t: threshold in negative dB,
katkost@1 5 % nH: maximum number of harmonics, minf0: minimum f0 frequency in Hz,
katkost@1 6 % maxf0: maximim f0 frequency in Hz,
katkost@1 7 % f0et: error threshold in the f0 detection (ex: 5),
katkost@1 8 % maxhd: max. relative deviation in harmonic detection (ex: .2)
katkost@1 9 % stocf: decimation factor of mag spectrum for stochastic analysis
katkost@1 10 % y: output sound, yh: harmonic component, ys: stochastic component
katkost@1 11 M = length(w); % analysis window size
katkost@1 12 Ns = 1024; % FFT size for synthesis
katkost@1 13 H = 256; % hop size for analysis and synthesis
katkost@1 14 N2 = N/2+1; % half-size of spectrum
katkost@1 15 soundlength = length(x); % length of input sound array
katkost@1 16 hNs = Ns/2; % half synthesis window size
katkost@1 17 hM = (M-1)/2; % half analysis window size
katkost@1 18 pin = max(hNs+1,1+hM); % initialize sound pointer to middle of analysis window
katkost@1 19 pend = soundlength-max(hM,hNs); % last sample to start a frame
katkost@1 20 fftbuffer = zeros(N,1); % initialize buffer for FFT
katkost@1 21 yh = zeros(soundlength+Ns/2,1); % output sine component
katkost@1 22 ys = zeros(soundlength+Ns/2,1); % output residual component
katkost@1 23 w = w/sum(w); % normalize analysis window
katkost@1 24 sw = zeros(Ns,1);
katkost@1 25 ow = triang(2*H-1); % overlapping window
katkost@1 26 ovidx = Ns/2+1-H+1:Ns/2+H; % overlap indexes
katkost@1 27 sw(ovidx) = ow(1:2*H-1);
katkost@1 28 bh = blackmanharris(Ns); % synthesis window
katkost@1 29 bh = bh ./ sum(bh); % normalize synthesis window
katkost@1 30 wr = bh; % window for residual
katkost@1 31 sw(ovidx) = sw(ovidx) ./ bh(ovidx);
katkost@1 32 sws = H*hanning(Ns); % synthesis window for stochastic
katkost@1 33
katkost@1 34 i = 0;
katkost@1 35 while pin<pend
katkost@1 36 i = i+1;
katkost@1 37 %-----analysis-----%
katkost@1 38 xw = x(pin-hM:pin+hM).*w(1:M); % window the input sound
katkost@1 39 fftbuffer(1:(M+1)/2) = xw((M+1)/2:M); % zero-phase window in fftbuffer
katkost@1 40 fftbuffer(N-(M-1)/2+1:N) = xw(1:(M-1)/2);
katkost@1 41 X = fft(fftbuffer); % compute the FFT
katkost@1 42 mX = 20*log10(abs(X(1:N2))); % magnitude spectrum
katkost@1 43 pX = unwrap(angle(X(1:N/2+1))); % unwrapped phase spectrum
katkost@1 44 ploc = 1 + find((mX(2:N2-1)>t) .* (mX(2:N2-1)>mX(3:N2)) ...
katkost@1 45 .* (mX(2:N2-1)>mX(1:N2-2))); % find peaks
katkost@1 46 [ploc,pmag,pphase] = peakinterp(mX,pX,ploc); % refine peak values
katkost@1 47 f0 = f0detection(mX,fs,ploc,pmag,f0et,minf0,maxf0); % find f0
katkost@1 48 fr0(i)=f0;
katkost@1 49 hloc = zeros(nH,1); % initialize harmonic locations
katkost@1 50 hmag = zeros(nH,1)-100; % initialize harmonic magnitudes
katkost@1 51 hphase = zeros(nH,1); % initialize harmonic phases
katkost@1 52 hf = (f0>0).*(f0.*(1:nH)); % initialize harmonic frequencies
katkost@1 53 hi = 1; % initialize harmonic index
katkost@1 54 npeaks = length(ploc); % number of peaks found
katkost@1 55 while (f0>0 && hi<=nH && hf(hi)<fs/2) % find harmonic peaks
katkost@1 56 [dev,pei] = min(abs((ploc(1:npeaks)-1)/N*fs-hf(hi))); % closest peak
katkost@1 57 if ((hi==1 || ~any(hloc(1:hi-1)==ploc(pei))) && dev<maxhd*hf(hi))
katkost@1 58 hloc(hi) = ploc(pei); % harmonic locations
katkost@1 59 hmag(hi) = pmag(pei); % harmonic magnitudes
katkost@1 60 hphase(hi) = pphase(pei); % harmonic phases
katkost@1 61 end
katkost@1 62 hi = hi+1; % increase harmonic index
katkost@1 63 end
katkost@1 64 hloc(1:hi-1) = (hloc(1:hi-1)~=0).*((hloc(1:hi-1)-1)*Ns/N+1); % synth. locs
katkost@1 65 ri= pin-hNs; % input sound pointer for residual analysis
katkost@1 66 xr = x(ri:ri+Ns-1).*wr(1:Ns); % window the input sound
katkost@1 67 Xr = fft(fftshift(xr)); % compute FFT for residual analysis
katkost@1 68 Yh = genspecsines(hloc(1:hi-1),hmag,hphase,Ns); % generate sines
katkost@1 69 Yr = Xr-Yh; % get the residual complex spectrum
katkost@1 70 mYr = abs(Yr(1:Ns/2+1)); % magnitude spectrum of residual
katkost@1 71 %mYs = stochenvelope(mYr,stocf);
katkost@1 72 %-----transformations-----%
katkost@1 73 mYsenv = decimate(mYr,stocf,1); % decimate the magnitude spectrum
katkost@1 74 %-----synthesis-----%
katkost@1 75 mYs = interp(mYsenv,stocf,1); % interpolate to original size
katkost@1 76
katkost@1 77 % n=1:N/2+1;
katkost@1 78 % plot(n/N*Ns,mX); %plotting the original spectrum
katkost@1 79 % hold on;
katkost@1 80 % plot(20*log10(abs(mYs)), 'r'); %plotting the approximation done by the decimate function
katkost@1 81 %
katkost@1 82 % hold on;
katkost@1 83 % plot(hloc, hmag, 'g*');
katkost@1 84 % hold off;
katkost@1 85 % pause
katkost@1 86
katkost@1 87 roffset = ceil(stocf/2)-1; % interpolated array offset
katkost@1 88 mYs = [ mYs(1)*ones(roffset,1); mYs(1:Ns/2+1-roffset) ];
katkost@1 89 pYs = 2*pi*rand(Ns/2+1,1); % generate phase random values
katkost@1 90 mYs1 = [mYs(1:Ns/2+1); mYs(Ns/2:-1:2)]; % create magnitude spectrum
katkost@1 91 pYs1 = [pYs(1:Ns/2+1); -1*pYs(Ns/2:-1:2)]; % create phase spectrum
katkost@1 92 Ys = mYs1.*cos(pYs1)+1i*mYs1.*sin(pYs1); % compute complex spectrum
katkost@1 93 yhw = fftshift(real(ifft(Yh))); % sines in time domain using IFFT
katkost@1 94 ysw = fftshift(real(ifft(Ys))); % stoc. in time domain using IFFT
katkost@1 95 yh(ri:ri+Ns-1) = yh(ri:ri+Ns-1)+yhw(1:Ns).*sw; % overlap-add for sines
katkost@1 96 ys(ri:ri+Ns-1) = ys(ri:ri+Ns-1)+ysw(1:Ns).*sws; % overlap-add for stoch.
katkost@1 97 pin = pin+H; % advance the sound pointer
katkost@1 98 end
katkost@1 99
katkost@1 100 %ys=tanh(10*ys);
katkost@1 101 y= yh+ys; % sum sines and stochastic