powerToDb function
Converts a power spectrogram (amplitude squared) to decibel (dB) units.
spectrogram: The input spectrogram (linear power).
topDb: The dynamic range of the output. Values below max - topDb are clipped.
Returns the spectrogram in dB.
Implementation
List<Float64List> powerToDb(
List<Float64List> spectrogram, {
double topDb = 80.0,
}) {
double maxVal = 0.0;
for (final row in spectrogram) {
for (final val in row) {
if (val > maxVal) maxVal = val;
}
}
const minThreshold = 1e-10;
final ref = maxVal > minThreshold ? maxVal : 1.0;
final log10 = log(10);
final floor = -topDb;
return spectrogram.map((row) {
final newRow = Float64List(row.length);
for (int i = 0; i < row.length; i++) {
final val = row[i] < minThreshold ? minThreshold : row[i];
final db = 10.0 * log(val / ref) / log10;
newRow[i] = db < floor ? floor : db;
}
return newRow;
}).toList();
}