annotate 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
rev   line source
holger@0 1 function shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT)
holger@0 2 % shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT)
holger@0 3 %
holger@0 4 % converts midi pitch to shift values in constant-Q vector.
holger@0 5 % shifts for pitches that below the lowest or above the highest CQT
holger@0 6 % frequency are set to a value of -1
holger@0 7
holger@0 8 numMidiPitches = length(midiPitches);
holger@0 9 numFreqsCQT = length(binFreqsCQT);
holger@0 10
holger@0 11 pitchF0s = midiPitch2Freq(midiPitches, tuningFreqInHz);
holger@0 12 outOfBoundsPitches = (pitchF0s < binFreqsCQT(1) | pitchF0s > binFreqsCQT(end));
holger@0 13 if any(outOfBoundsPitches)
holger@0 14 warning('F0s of some midi pitches lie outside the constant-Q spectrogram frequency range');
holger@0 15 end
holger@0 16
holger@0 17 freqRatios = repmat(binFreqsCQT, 1, numMidiPitches) ./ repmat(pitchF0s', numFreqsCQT, 1);
holger@0 18 freqRatios(freqRatios < 1) = 1 ./ freqRatios(freqRatios < 1);
holger@0 19
holger@0 20 [dummy shifts] = min(abs(freqRatios), [], 1);
holger@0 21 shifts = shifts - 1; % 'no shift' is defined as 0
holger@0 22 shifts(outOfBoundsPitches) = -1;
holger@0 23 shifts = shifts';