2022-04-28 13:38:07 +02:00
|
|
|
#include "waterfall_plot.h"
|
|
|
|
|
#include "imgui/imgui_internal.h"
|
|
|
|
|
#include <string>
|
|
|
|
|
#include "imgui/imgui_image.h"
|
|
|
|
|
#include "resources.h"
|
2024-02-02 23:15:20 -05:00
|
|
|
#include "logger.h"
|
2022-04-28 13:38:07 +02:00
|
|
|
|
|
|
|
|
namespace widgets
|
|
|
|
|
{
|
2023-02-11 01:07:39 +01:00
|
|
|
WaterfallPlot::WaterfallPlot(int size, int lines)
|
2022-04-28 13:38:07 +02:00
|
|
|
{
|
2022-11-29 00:01:26 +01:00
|
|
|
fft_max_size = fft_size = size;
|
2022-04-28 13:38:07 +02:00
|
|
|
fft_lines = lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WaterfallPlot::~WaterfallPlot()
|
|
|
|
|
{
|
2023-02-11 01:07:39 +01:00
|
|
|
if (raw_img_buffer != nullptr)
|
2024-02-02 23:15:20 -05:00
|
|
|
free(raw_img_buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WaterfallPlot::buffer_alloc(size_t size)
|
|
|
|
|
{
|
|
|
|
|
uint32_t* new_img_buffer = (uint32_t*)realloc(raw_img_buffer, size);
|
|
|
|
|
if (new_img_buffer == nullptr)
|
|
|
|
|
{
|
|
|
|
|
logger->error("Cannot allocate memory for waterfall");
|
|
|
|
|
if (raw_img_buffer != nullptr)
|
|
|
|
|
{
|
|
|
|
|
free(raw_img_buffer);
|
|
|
|
|
raw_img_buffer = nullptr;
|
|
|
|
|
}
|
|
|
|
|
last_curr_height = last_curr_width = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
raw_img_buffer = new_img_buffer;
|
|
|
|
|
uint64_t old_size = last_curr_width * last_curr_height;
|
|
|
|
|
if (size > old_size * sizeof(uint32_t))
|
|
|
|
|
memset(&raw_img_buffer[old_size], 0, size - old_size * sizeof(uint32_t));
|
|
|
|
|
last_curr_width = curr_width;
|
|
|
|
|
last_curr_height = curr_height;
|
|
|
|
|
return true;
|
2022-04-28 13:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-08 13:01:05 +02:00
|
|
|
void WaterfallPlot::draw(ImVec2 size, bool active)
|
2022-04-28 13:38:07 +02:00
|
|
|
{
|
2024-02-08 14:18:45 -05:00
|
|
|
work_mtx.lock();
|
2024-02-24 21:35:29 -05:00
|
|
|
if (texture_id == 0 || active)
|
|
|
|
|
{
|
|
|
|
|
curr_width = size.x > fft_size ? fft_size : size.x;
|
|
|
|
|
curr_height = size.y > fft_lines ? fft_lines : size.y;
|
|
|
|
|
}
|
2023-11-06 11:23:20 -05:00
|
|
|
if (texture_id == 0)
|
2022-04-28 13:38:07 +02:00
|
|
|
{
|
2023-11-06 11:23:20 -05:00
|
|
|
texture_id = makeImageTexture();
|
2024-02-02 23:15:20 -05:00
|
|
|
need_update = buffer_alloc(curr_width * curr_height * sizeof(uint32_t));
|
2023-11-06 11:23:20 -05:00
|
|
|
if ((int)palette.size() != resolution)
|
|
|
|
|
set_palette(colormaps::loadMap(resources::getResourcePath("waterfall/classic.json")), false);
|
|
|
|
|
}
|
|
|
|
|
if (active && (last_curr_width != curr_width || last_curr_height != curr_height))
|
|
|
|
|
{
|
2024-02-02 23:15:20 -05:00
|
|
|
if (raw_img_buffer != nullptr && last_curr_width != curr_width)
|
|
|
|
|
{
|
|
|
|
|
free(raw_img_buffer);
|
|
|
|
|
raw_img_buffer = nullptr;
|
|
|
|
|
last_curr_height = last_curr_width = 0;
|
|
|
|
|
}
|
|
|
|
|
need_update = buffer_alloc(curr_width * curr_height * sizeof(uint32_t));
|
2023-11-06 11:23:20 -05:00
|
|
|
}
|
|
|
|
|
if (need_update)
|
|
|
|
|
{
|
|
|
|
|
updateImageTexture(texture_id, raw_img_buffer, curr_width, curr_height);
|
2023-02-12 21:20:27 +01:00
|
|
|
need_update = false;
|
2023-02-11 01:07:39 +01:00
|
|
|
}
|
2023-11-06 11:23:20 -05:00
|
|
|
work_mtx.unlock();
|
2023-02-11 01:07:39 +01:00
|
|
|
|
|
|
|
|
ImGui::Image((void *)(intptr_t)texture_id, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WaterfallPlot::push_fft(float *values)
|
|
|
|
|
{
|
2024-02-02 23:15:20 -05:00
|
|
|
if (texture_id == 0 || raw_img_buffer == nullptr)
|
2023-02-12 21:20:27 +01:00
|
|
|
return;
|
|
|
|
|
|
2023-02-11 01:07:39 +01:00
|
|
|
work_mtx.lock();
|
|
|
|
|
if ((waterfall_i++ % waterfall_i_mod) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (waterfall_i * 5e6 == waterfall_i_mod)
|
|
|
|
|
waterfall_i = 0;
|
|
|
|
|
|
2023-02-10 23:10:24 +01:00
|
|
|
memmove(&raw_img_buffer[curr_width * 1], &raw_img_buffer[curr_width * 0], curr_width * (curr_height - 1) * sizeof(uint32_t));
|
2022-04-28 13:38:07 +02:00
|
|
|
|
2023-02-10 23:10:24 +01:00
|
|
|
double fz = (double)fft_size / (double)curr_width;
|
|
|
|
|
for (int i = 0; i < curr_width; i++)
|
2022-05-08 13:01:05 +02:00
|
|
|
{
|
2023-02-10 23:10:24 +01:00
|
|
|
float ffpos = i * fz;
|
|
|
|
|
|
|
|
|
|
if (ffpos >= fft_size)
|
|
|
|
|
ffpos = fft_size - 1;
|
|
|
|
|
|
|
|
|
|
float final = -INFINITY;
|
|
|
|
|
for (float v = ffpos; v < ffpos + fz; v += 1)
|
|
|
|
|
if (final < values[(int)floor(v)])
|
|
|
|
|
final = values[(int)floor(v)];
|
|
|
|
|
|
|
|
|
|
int v = ((final - scale_min) / fabs(scale_max - scale_min)) * resolution;
|
2022-04-28 13:38:07 +02:00
|
|
|
|
2022-05-08 13:01:05 +02:00
|
|
|
if (v < 0)
|
|
|
|
|
v = 0;
|
2022-10-27 09:54:52 +02:00
|
|
|
if (v >= resolution)
|
|
|
|
|
v = resolution - 1;
|
2022-04-28 13:38:07 +02:00
|
|
|
|
2022-05-08 13:01:05 +02:00
|
|
|
raw_img_buffer[i] = palette[v];
|
|
|
|
|
}
|
2023-02-12 21:20:27 +01:00
|
|
|
|
|
|
|
|
need_update = true;
|
2022-05-08 13:01:05 +02:00
|
|
|
}
|
2023-02-11 01:07:39 +01:00
|
|
|
work_mtx.unlock();
|
|
|
|
|
}
|
2022-04-28 13:38:07 +02:00
|
|
|
|
2023-02-11 01:07:39 +01:00
|
|
|
void WaterfallPlot::set_rate(int input_rate, int output_rate)
|
|
|
|
|
{
|
|
|
|
|
work_mtx.lock();
|
|
|
|
|
if (output_rate <= 0)
|
|
|
|
|
output_rate = 1;
|
|
|
|
|
waterfall_i_mod = input_rate / output_rate;
|
|
|
|
|
if (waterfall_i_mod <= 0)
|
|
|
|
|
waterfall_i_mod = 1;
|
|
|
|
|
waterfall_i = 0;
|
2022-11-29 00:01:26 +01:00
|
|
|
work_mtx.unlock();
|
2022-04-28 13:38:07 +02:00
|
|
|
}
|
2022-11-29 20:15:06 +01:00
|
|
|
|
|
|
|
|
void WaterfallPlot::set_palette(colormaps::Map pa, bool mtx)
|
|
|
|
|
{
|
|
|
|
|
if (mtx)
|
|
|
|
|
work_mtx.lock();
|
|
|
|
|
palette = colormaps::generatePalette(pa, resolution);
|
|
|
|
|
if (mtx)
|
|
|
|
|
work_mtx.unlock();
|
|
|
|
|
}
|
2022-04-28 13:38:07 +02:00
|
|
|
}
|