Skip to content

Commit

Permalink
Merge pull request #305 from Renumics/fix/204-spectrogram-decibel-colors
Browse files Browse the repository at this point in the history
Fix/204 spectrogram decibel colors
  • Loading branch information
neindochoh authored Oct 25, 2023
2 parents 232e022 + 4b46a75 commit d16dbb3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
7 changes: 1 addition & 6 deletions src/lenses/SpectrogramLens/SpectrogramWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,7 @@ const calculateFrequencies = (
tables.cosTable
);

const array = new Uint8Array(fftSamples / 2);
let j;
for (j = 0; j < fftSamples / 2; j++) {
array[j] = Math.max(-255, Math.log10(spectrum[j]) * 45);
}
channelFreq.push(array);
channelFreq.push(spectrum);
currentOffset += fftSamples - noverlap;
}
frequencies.push(channelFreq);
Expand Down
29 changes: 18 additions & 11 deletions src/lenses/SpectrogramLens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ColorsState, useColors } from '../../stores/colors';
import { Lens } from '../../types';
import useSetting from '../useSetting';
import MenuBar from './MenuBar';
import chroma from 'chroma-js';
import { fixWindow, freqType, unitType, amplitudeToDb } from './Spectrogram';

const Container = tw.div`flex flex-col w-full h-full items-stretch justify-center`;
Expand Down Expand Up @@ -209,7 +208,8 @@ const SpectrogramLens: Lens = ({ columns, urls, values }) => {
const widthScale = d3.scaleLinear([0, width], [0, frequenciesData.length]);

let drawData = [];
let colorScale: chroma.Scale<chroma.Color>;
let min = 0;
let max = 0;

if (ampScale === 'decibel') {
let ref = 0;
Expand All @@ -223,9 +223,6 @@ const SpectrogramLens: Lens = ({ columns, urls, values }) => {
//const top_db = 80;
const amin = 1e-5;

let log_spec_max = 0;
let log_spec_min = 0;

// Convert amplitudes to decibels
for (let i = 0; i < frequenciesData.length; i++) {
const col = [];
Expand All @@ -234,23 +231,33 @@ const SpectrogramLens: Lens = ({ columns, urls, values }) => {
const amplitude = frequenciesData[i][j];
col[j] = amplitudeToDb(amplitude, ref, amin);

if (col[j] > log_spec_max) {
log_spec_max = col[j];
if (col[j] > max) {
max = col[j];
}

if (col[j] < log_spec_min) {
log_spec_min = col[j];
if (col[j] < min) {
min = col[j];
}
}

drawData[i] = col;
}
colorScale = colorPalette.scale().domain([log_spec_min, log_spec_max]);
} else {
// ampScale === 'linear'
colorScale = colorPalette.scale().domain([0, 256]);
for (let i = 0; i < frequenciesData.length; i++) {
const maxI = Math.max(...frequenciesData[i]);
const minI = Math.min(...frequenciesData[i]);

if (maxI > max) {
max = maxI;
}
if (minI < min) {
min = minI;
}
}
drawData = frequenciesData;
}
const colorScale = colorPalette.scale().domain([min, max]);

for (let y = 0; y < height; y++) {
let value = 0;
Expand Down

0 comments on commit d16dbb3

Please sign in to comment.