diff functions/midiPitch2Shift.m @ 0:b4e26b53072f tip

Initial commit.
author Holger Kirchhoff <holger.kirchhoff@eecs.qmul.ac.uk>
date Tue, 04 Dec 2012 13:57:15 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions/midiPitch2Shift.m	Tue Dec 04 13:57:15 2012 +0000
@@ -0,0 +1,23 @@
+function shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT)
+% shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT)
+% 
+% converts midi pitch to shift values in constant-Q vector.
+% shifts for pitches that below the lowest or above the highest CQT
+% frequency are set to a value of -1
+
+numMidiPitches = length(midiPitches);
+numFreqsCQT    = length(binFreqsCQT);
+
+pitchF0s = midiPitch2Freq(midiPitches, tuningFreqInHz);
+outOfBoundsPitches = (pitchF0s < binFreqsCQT(1) | pitchF0s > binFreqsCQT(end));
+if any(outOfBoundsPitches)
+    warning('F0s of some midi pitches lie outside the constant-Q spectrogram frequency range');
+end
+   
+freqRatios = repmat(binFreqsCQT, 1, numMidiPitches) ./ repmat(pitchF0s', numFreqsCQT, 1);
+freqRatios(freqRatios < 1) = 1 ./ freqRatios(freqRatios < 1);
+
+[dummy shifts] = min(abs(freqRatios), [], 1);
+shifts = shifts - 1; % 'no shift' is defined as 0
+shifts(outOfBoundsPitches) = -1;
+shifts = shifts';
\ No newline at end of file