Skip to content

Commit 640aa1a

Browse files
authored
Merge pull request #9 from MichaelBell/log-scale-Y
Log scale Y axis
2 parents 92f3b58 + 70489f1 commit 640aa1a

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

src/btstack_audio_pico.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,25 @@ static void btstack_audio_pico_sink_fill_buffers(void){
252252
}
253253
}
254254

255+
// Choose one:
256+
//#define SCALE_LOGARITHMIC
257+
#define SCALE_SQRT
258+
//#define SCALE_LINEAR
259+
260+
constexpr float max_sample_from_fft = 4000.f + 130.f * display.HEIGHT;
261+
constexpr int lower_threshold = 270 - 2 * display.HEIGHT;
262+
#ifdef SCALE_LOGARITHMIC
263+
constexpr fix15 multiple = float_to_fix15(pow(max_sample_from_fft / lower_threshold, -1.f / (display.HEIGHT - 1)));
264+
#elif defined(SCALE_SQRT)
265+
constexpr fix15 subtract_step = float_to_fix15((max_sample_from_fft - lower_threshold) * 2.f / (display.HEIGHT * (display.HEIGHT - 1)));
266+
#elif defined(SCALE_LINEAR)
267+
constexpr fix15 subtract = float_to_fix15((max_sample_from_fft - lower_threshold) / (display.HEIGHT - 1));
268+
#else
269+
#error "Choose a scale mode"
270+
#endif
255271
fft.update();
256272
for (auto i = 0u; i < display.WIDTH; i++) {
257-
uint16_t sample = std::min((int16_t)(display.HEIGHT * 255), (int16_t)fft.get_scaled_fix15(i + FFT_SKIP_BINS, loudness_adjust[i]));
273+
fix15 sample = std::min(float_to_fix15(max_sample_from_fft), fft.get_scaled_as_fix15(i + FFT_SKIP_BINS, loudness_adjust[i]));
258274
uint8_t maxy = 0;
259275

260276
for (int j = 0; j < HISTORY_LEN; ++j) {
@@ -263,20 +279,31 @@ static void btstack_audio_pico_sink_fill_buffers(void){
263279
}
264280
}
265281

282+
#ifdef SCALE_SQRT
283+
fix15 subtract = subtract_step;
284+
#endif
266285
for (auto y = 0; y < display.HEIGHT; y++) {
267286
uint8_t r = 0;
268287
uint8_t g = 0;
269288
uint8_t b = 0;
270-
if (sample > 255) {
289+
if (sample > int_to_fix15(lower_threshold)) {
271290
r = (uint16_t)(palette_main[i].r);
272291
g = (uint16_t)(palette_main[i].g);
273292
b = (uint16_t)(palette_main[i].b);
274-
sample -= 255;
293+
#ifdef SCALE_LOGARITHMIC
294+
sample = multiply_fix15_unit(multiple, sample);
295+
#else
296+
sample = std::max(1, sample - subtract);
297+
#ifdef SCALE_SQRT
298+
subtract += subtract_step;
299+
#endif
300+
#endif
275301
}
276302
else if (sample > 0) {
277-
r = std::min((uint16_t)(palette_main[i].r), sample);
278-
g = std::min((uint16_t)(palette_main[i].g), sample);
279-
b = std::min((uint16_t)(palette_main[i].b), sample);
303+
uint16_t int_sample = (uint16_t)fix15_to_int(sample);
304+
r = std::min((uint16_t)(palette_main[i].r), int_sample);
305+
g = std::min((uint16_t)(palette_main[i].g), int_sample);
306+
b = std::min((uint16_t)(palette_main[i].b), int_sample);
280307
eq_history[i][history_idx] = y;
281308
sample = 0;
282309
if (maxy < y) {

src/fixed_fft.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ int FIX_FFT::get_scaled_fix15(unsigned int i, fix15 scale) {
2828
return fix15_to_int(multiply_fix15(fr[i], scale));
2929
}
3030

31+
int FIX_FFT::get_scaled_as_fix15(unsigned int i, fix15 scale) {
32+
return multiply_fix15(fr[i], scale);
33+
}
34+
3135
void FIX_FFT::init() {
3236

3337
// Populate Filter and Sine tables
@@ -75,13 +79,6 @@ float FIX_FFT::max_frequency() {
7579
return max_freq_dex * (sample_rate / SAMPLE_COUNT);
7680
}
7781

78-
// abs(a) must be <= 1
79-
constexpr __always_inline fix15 multiply_fix15_unit(fix15 a, fix15 b) {
80-
int32_t bh = b >> 15;
81-
int32_t bl = b & 0x7fff;
82-
return ((a * bl) >> 15) + (a * bh);;
83-
}
84-
8582
void FIX_FFT::FFT() {
8683
// Bit Reversal Permutation
8784
// Bit reversal code below originally based on that found here:

src/fixed_fft.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ constexpr __always_inline float fix15_to_float(fix15 a) {return (float)(a) / 327
1414
constexpr __always_inline fix15 int_to_fix15(int a) {return (fix15)(a << 15);}
1515
constexpr __always_inline int fix15_to_int(fix15 a) {return (int)(a >> 15);}
1616

17+
// abs(a) must be <= 1
18+
constexpr __always_inline fix15 multiply_fix15_unit(fix15 a, fix15 b) {
19+
int32_t bh = b >> 15;
20+
int32_t bl = b & 0x7fff;
21+
return ((a * bl) >> 15) + (a * bh);;
22+
}
23+
1724
constexpr unsigned int SAMPLE_COUNT = 1024u;
1825

1926
class FIX_FFT {
@@ -56,4 +63,5 @@ class FIX_FFT {
5663
float max_frequency();
5764
int get_scaled(unsigned int i, unsigned int scale);
5865
int get_scaled_fix15(unsigned int i, fix15 scale);
66+
fix15 get_scaled_as_fix15(unsigned int i, fix15 scale);
5967
};

0 commit comments

Comments
 (0)