holger@0: function shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT) holger@0: % shifts = midiPitch2Shift(midiPitches, tuningFreqInHz, binFreqsCQT) holger@0: % holger@0: % converts midi pitch to shift values in constant-Q vector. holger@0: % shifts for pitches that below the lowest or above the highest CQT holger@0: % frequency are set to a value of -1 holger@0: holger@0: numMidiPitches = length(midiPitches); holger@0: numFreqsCQT = length(binFreqsCQT); holger@0: holger@0: pitchF0s = midiPitch2Freq(midiPitches, tuningFreqInHz); holger@0: outOfBoundsPitches = (pitchF0s < binFreqsCQT(1) | pitchF0s > binFreqsCQT(end)); holger@0: if any(outOfBoundsPitches) holger@0: warning('F0s of some midi pitches lie outside the constant-Q spectrogram frequency range'); holger@0: end holger@0: holger@0: freqRatios = repmat(binFreqsCQT, 1, numMidiPitches) ./ repmat(pitchF0s', numFreqsCQT, 1); holger@0: freqRatios(freqRatios < 1) = 1 ./ freqRatios(freqRatios < 1); holger@0: holger@0: [dummy shifts] = min(abs(freqRatios), [], 1); holger@0: shifts = shifts - 1; % 'no shift' is defined as 0 holger@0: shifts(outOfBoundsPitches) = -1; holger@0: shifts = shifts';