katkost@1: function [y,yh,ys,fr0] = hpsmodel(x,fs,w,N,t,nH,minf0,maxf0,f0et,maxhd,stocf) katkost@1: %=> analysis/synthesis of a sound using the sinusoidal harmonic model katkost@1: % x: input sound, fs: sampling rate, w: analysis window (odd size), katkost@1: % N: FFT size (minimum 512), t: threshold in negative dB, katkost@1: % nH: maximum number of harmonics, minf0: minimum f0 frequency in Hz, katkost@1: % maxf0: maximim f0 frequency in Hz, katkost@1: % f0et: error threshold in the f0 detection (ex: 5), katkost@1: % maxhd: max. relative deviation in harmonic detection (ex: .2) katkost@1: % stocf: decimation factor of mag spectrum for stochastic analysis katkost@1: % y: output sound, yh: harmonic component, ys: stochastic component katkost@1: M = length(w); % analysis window size katkost@1: Ns = 1024; % FFT size for synthesis katkost@1: H = 256; % hop size for analysis and synthesis katkost@1: N2 = N/2+1; % half-size of spectrum katkost@1: soundlength = length(x); % length of input sound array katkost@1: hNs = Ns/2; % half synthesis window size katkost@1: hM = (M-1)/2; % half analysis window size katkost@1: pin = max(hNs+1,1+hM); % initialize sound pointer to middle of analysis window katkost@1: pend = soundlength-max(hM,hNs); % last sample to start a frame katkost@1: fftbuffer = zeros(N,1); % initialize buffer for FFT katkost@1: yh = zeros(soundlength+Ns/2,1); % output sine component katkost@1: ys = zeros(soundlength+Ns/2,1); % output residual component katkost@1: w = w/sum(w); % normalize analysis window katkost@1: sw = zeros(Ns,1); katkost@1: ow = triang(2*H-1); % overlapping window katkost@1: ovidx = Ns/2+1-H+1:Ns/2+H; % overlap indexes katkost@1: sw(ovidx) = ow(1:2*H-1); katkost@1: bh = blackmanharris(Ns); % synthesis window katkost@1: bh = bh ./ sum(bh); % normalize synthesis window katkost@1: wr = bh; % window for residual katkost@1: sw(ovidx) = sw(ovidx) ./ bh(ovidx); katkost@1: sws = H*hanning(Ns); % synthesis window for stochastic katkost@1: katkost@1: i = 0; katkost@1: while pint) .* (mX(2:N2-1)>mX(3:N2)) ... katkost@1: .* (mX(2:N2-1)>mX(1:N2-2))); % find peaks katkost@1: [ploc,pmag,pphase] = peakinterp(mX,pX,ploc); % refine peak values katkost@1: f0 = f0detection(mX,fs,ploc,pmag,f0et,minf0,maxf0); % find f0 katkost@1: fr0(i)=f0; katkost@1: hloc = zeros(nH,1); % initialize harmonic locations katkost@1: hmag = zeros(nH,1)-100; % initialize harmonic magnitudes katkost@1: hphase = zeros(nH,1); % initialize harmonic phases katkost@1: hf = (f0>0).*(f0.*(1:nH)); % initialize harmonic frequencies katkost@1: hi = 1; % initialize harmonic index katkost@1: npeaks = length(ploc); % number of peaks found katkost@1: while (f0>0 && hi<=nH && hf(hi)