Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: [Spectrogram] Resample spectrogram at Mel frequencies without ch… #3861

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/plugins/spectrogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,20 +537,16 @@ class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramP
private createMelFilterBank(numMelFilters, sampleRate) {
const melMin = this.hzToMel(0)
const melMax = this.hzToMel(sampleRate / 2)
const melPoints = []
for (let i = 0; i <= numMelFilters + 1; i++) {
melPoints.push(this.melToHz(melMin + (i / (numMelFilters + 1)) * (melMax - melMin)))
}
const melFilterBank = Array.from({ length: numMelFilters }, () => Array(this.fftSamples / 2 + 1).fill(0))
for (let i = 1; i <= numMelFilters; i++) {
for (let j = 0; j < this.fftSamples / 2 + 1; j++) {
const freq = j * (sampleRate / this.fftSamples)
if (freq >= melPoints[i - 1] && freq <= melPoints[i]) {
melFilterBank[i - 1][j] = (freq - melPoints[i - 1]) / (melPoints[i] - melPoints[i - 1])
} else if (freq >= melPoints[i] && freq <= melPoints[i + 1]) {
melFilterBank[i - 1][j] = (melPoints[i + 1] - freq) / (melPoints[i + 1] - melPoints[i])
}
}
const scale = (sampleRate / this.fftSamples)
for (let i = 0; i < numMelFilters; i++) {
let hz = this.melToHz(melMin + (i / numMelFilters) * (melMax - melMin))
let j = Math.floor(hz / scale)
let hzLow = j * scale
let hzHigh = (j + 1) * scale
let r = (hz - hzLow) / (hzHigh - hzLow)
melFilterBank[i][j] = 1 - r
melFilterBank[i][j + 1] = r
}
return melFilterBank
}
Expand Down
Loading