annotate src/matlab/ifptrack.m @ 0:c52bc3e8d3ad tip

user: boblsturm branch 'default' added README.md added assets/.DS_Store added assets/playButton.jpg added assets/stopButton.png added assets/swapButton.jpg added data/.DS_Store added data/fiveoctaves.mp3 added data/glock2.wav added data/sinScale.mp3 added data/speech_female.mp3 added data/sweep.wav added nimfks.m.lnk added src/.DS_Store added src/matlab/.DS_Store added src/matlab/AnalysisCache.m added src/matlab/CSS.m added src/matlab/DataHash.m added src/matlab/ExistsInCache.m added src/matlab/KLDivCost.m added src/matlab/LoadFromCache.m added src/matlab/SA_B_NMF.m added src/matlab/SaveInCache.m added src/matlab/Sound.m added src/matlab/SynthesisCache.m added src/matlab/chromagram_E.m added src/matlab/chromagram_IF.m added src/matlab/chromagram_P.m added src/matlab/chromsynth.m added src/matlab/computeSTFTFeat.m added src/matlab/controller.m added src/matlab/decibelSliderReleaseCallback.m added src/matlab/drawClickCallBack.m added src/matlab/fft2chromamx.m added src/matlab/hz2octs.m added src/matlab/ifgram.m added src/matlab/ifptrack.m added src/matlab/istft.m added src/matlab/nimfks.fig added src/matlab/nimfks.m added src/matlab/nmfFn.m added src/matlab/nmf_beta.m added src/matlab/nmf_divergence.m added src/matlab/nmf_euclidean.m added src/matlab/prune_corpus.m added src/matlab/rot_kernel.m added src/matlab/templateAdditionResynth.m added src/matlab/templateDelCb.m added src/matlab/templateScrollCb.m
author boblsturm
date Sun, 18 Jun 2017 06:26:13 -0400
parents
children
rev   line source
boblsturm@0 1 function [p,m,S] = ifptrack(d,w,sr,fminl,fminu,fmaxl,fmaxu)
boblsturm@0 2 % [p,m,S] = ifptrack(d,w,sr,fminl,fminu,fmaxl,fmaxu)
boblsturm@0 3 % Pitch track based on inst freq.
boblsturm@0 4 % Look for adjacent bins with same inst freq.
boblsturm@0 5 % d is the input waveform. sr is its sample rate
boblsturm@0 6 % w is the basic STFT DFT length (window is half, hop is 1/4)
boblsturm@0 7 % S returns the underlying complex STFT.
boblsturm@0 8 % fmin,fmax define ramps at edge of sensitivity
boblsturm@0 9 % 2006-05-03 dpwe@ee.columbia.edu
boblsturm@0 10
boblsturm@0 11 % Copyright (c) 2006 Columbia University.
boblsturm@0 12 %
boblsturm@0 13 % This file is part of LabROSA-coversongID
boblsturm@0 14 %
boblsturm@0 15 % LabROSA-coversongID is free software; you can redistribute it and/or modify
boblsturm@0 16 % it under the terms of the GNU General Public License version 2 as
boblsturm@0 17 % published by the Free Software Foundation.
boblsturm@0 18 %
boblsturm@0 19 % LabROSA-coversongID is distributed in the hope that it will be useful, but
boblsturm@0 20 % WITHOUT ANY WARRANTY; without even the implied warranty of
boblsturm@0 21 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
boblsturm@0 22 % General Public License for more details.
boblsturm@0 23 %
boblsturm@0 24 % You should have received a copy of the GNU General Public License
boblsturm@0 25 % along with LabROSA-coversongID; if not, write to the Free Software
boblsturm@0 26 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
boblsturm@0 27 % 02110-1301 USA
boblsturm@0 28 %
boblsturm@0 29 % See the file "COPYING" for the text of the license.
boblsturm@0 30
boblsturm@0 31 % downweight fundamentals below here
boblsturm@0 32 if nargin < 4; fminl = 150; end
boblsturm@0 33 if nargin < 5; fminu = 300; end
boblsturm@0 34 % highest frequency we look to
boblsturm@0 35 if nargin < 6; fmaxl = 2000; end
boblsturm@0 36 if nargin < 7; fmaxu = 4000; end
boblsturm@0 37
boblsturm@0 38
boblsturm@0 39 % Calculate the inst freq gram
boblsturm@0 40 [I,S] = ifgram(d,w,w/2,w/4,sr);
boblsturm@0 41
boblsturm@0 42 % Only look at bins up to 2 kHz
boblsturm@0 43 maxbin = round(fmaxu * (w/sr) );
boblsturm@0 44 %maxbin = size(I,1)
boblsturm@0 45 minbin = round(fminl * (w/sr) );
boblsturm@0 46
boblsturm@0 47 % Find plateaus in ifgram - stretches where delta IF is < thr
boblsturm@0 48 ddif = [I(2:maxbin, :);I(maxbin,:)] - [I(1,:);I(1:(maxbin-1),:)];
boblsturm@0 49
boblsturm@0 50 % expected increment per bin = sr/w, threshold at 3/4 that
boblsturm@0 51 dgood = abs(ddif) < .75*sr/w;
boblsturm@0 52
boblsturm@0 53 % delete any single bins (both above and below are zero);
boblsturm@0 54 dgood = dgood .* ([dgood(2:maxbin,:);dgood(maxbin,:)] > 0 | [dgood(1,:);dgood(1:(maxbin-1),:)] > 0);
boblsturm@0 55
boblsturm@0 56 % check it out
boblsturm@0 57 %p = dgood;
boblsturm@0 58
boblsturm@0 59 % reconstruct just pitchy cells?
boblsturm@0 60 %r = istft(p.*S,w,w/2,w/4);
boblsturm@0 61
boblsturm@0 62 p = 0*dgood;
boblsturm@0 63 m = 0*dgood;
boblsturm@0 64
boblsturm@0 65 % For each frame, extract all harmonic freqs & magnitudes
boblsturm@0 66 for t = 1:size(I,2)
boblsturm@0 67 ds = dgood(:,t)';
boblsturm@0 68 lds = length(ds);
boblsturm@0 69 % find nonzero regions in this vector
boblsturm@0 70 st = find(([0,ds(1:(lds-1))]==0) & (ds > 0));
boblsturm@0 71 en = find((ds > 0) & ([ds(2:lds),0] == 0));
boblsturm@0 72 npks = length(st);
boblsturm@0 73 frqs = zeros(1,npks);
boblsturm@0 74 mags = zeros(1,npks);
boblsturm@0 75 for i = 1:length(st)
boblsturm@0 76 bump = abs(S(st(i):en(i),t));
boblsturm@0 77 frqs(i) = (bump'*I(st(i):en(i),t))/(sum(bump)+(sum(bump)==0));
boblsturm@0 78 mags(i) = sum(bump);
boblsturm@0 79 if frqs(i) > fmaxu
boblsturm@0 80 mags(i) = 0;
boblsturm@0 81 frqs(i) = 0;
boblsturm@0 82 elseif frqs(i) > fmaxl
boblsturm@0 83 mags(i) = mags(i) * max(0, (fmaxu - frqs(i))/(fmaxu-fmaxl));
boblsturm@0 84 end
boblsturm@0 85 % downweight magnitudes below? 200 Hz
boblsturm@0 86 if frqs(i) < fminl
boblsturm@0 87 mags(i) = 0;
boblsturm@0 88 frqs(i) = 0;
boblsturm@0 89 elseif frqs(i) < fminu
boblsturm@0 90 % 1 octave fade-out
boblsturm@0 91 mags(i) = mags(i) * (frqs(i) - fminl)/(fminu-fminl);
boblsturm@0 92 end
boblsturm@0 93 if frqs(i) < 0
boblsturm@0 94 mags(i) = 0;
boblsturm@0 95 frqs(i) = 0;
boblsturm@0 96 end
boblsturm@0 97
boblsturm@0 98 end
boblsturm@0 99
boblsturm@0 100 % then just keep the largest at each frame (for now)
boblsturm@0 101 % [v,ix] = max(mags);
boblsturm@0 102 % p(t) = frqs(ix);
boblsturm@0 103 % m(t) = mags(ix);
boblsturm@0 104 % No, keep them all
boblsturm@0 105 %bin = st;
boblsturm@0 106 bin = round((st+en)/2);
boblsturm@0 107 p(bin,t) = frqs;
boblsturm@0 108 m(bin,t) = mags;
boblsturm@0 109 end
boblsturm@0 110
boblsturm@0 111 %% Pull out the max in each column
boblsturm@0 112 %[mm,ix] = max(m);
boblsturm@0 113 %% idiom to retrieve different element from each column
boblsturm@0 114 %[nr,nc] = size(p);
boblsturm@0 115 %pp = p((nr*[0:(nc-1)])+ix);
boblsturm@0 116 %mm = m((nr*[0:(nc-1)])+ix);
boblsturm@0 117 % r = synthtrax(pp,mm,sr,w/4);
boblsturm@0 118
boblsturm@0 119 %p = pp;
boblsturm@0 120 %m = mm;
boblsturm@0 121