1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#include "fft_plot.h"
#include "imgui/imgui_internal.h"
#include <string>
#include "common/dsp_source_sink/format_notated.h"
#include "core/style.h"
#include "resources.h"
#include "common/image/text.h"
namespace widgets
{
FFTPlot::FFTPlot(float *v, int size, float min, float max, float scale_res)
{
values = v;
values_size = size;
scale_min = min;
scale_max = max;
scale_resolution = scale_res;
}
void FFTPlot::draw(ImVec2 size)
{
work_mutex.lock();
ImGuiWindow *window = ImGui::GetCurrentWindow();
ImGuiContext &g = *GImGui;
const ImGuiStyle &style = g.Style;
const ImRect frame_bb(window->DC.CursorPos, {window->DC.CursorPos.x + size.x, window->DC.CursorPos.y + size.y});
const ImRect inner_bb({frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y},
{frame_bb.Max.x - 1, frame_bb.Max.y - style.FramePadding.y});
int res_w = size.x; // /*ImMin((int)size.x,*/ values_size /*)*/ - 1;
// int item_count = values_size - 1;
const float t_step = 1.0f / (float)res_w;
const float inv_scale = (scale_min == scale_max) ? 0.0f : (1.0f / (scale_max - scale_min));
float v0 = values[0];
float t0 = 0.0f;
ImVec2 tp0 = ImVec2(t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale));
const ImU32 col_base = ImGui::GetColorU32(ImGuiCol_PlotLines);
// Background
ImGui::RenderFrame(frame_bb.Min, frame_bb.Max, style::theme.plot_bg, true, style.FrameRounding);
// Draw lines
float vscale = ((scale_max - scale_min) / scale_resolution);
float step = (frame_bb.Max.y - frame_bb.Min.y) / scale_resolution;
float value = scale_min;
for (float i = frame_bb.Max.y - step; i >= frame_bb.Min.y; i -= step)
{
ImVec2 pos0 = {frame_bb.Min.x, i};
ImVec2 pos1 = {frame_bb.Max.x, i};
window->DrawList->AddLine(pos0, pos1, style::theme.fft_graduations);
value += vscale;
window->DrawList->AddText({pos0.x, pos0.y + 2}, style::theme.fft_graduations, std::string(std::to_string(int(value)) + " dB").c_str());
}
// Draw Freq Scale
// const ImU32 fcolor_scale = ImGui::GetColorU32(ImGuiCol_Text, 1.0);
if (enable_freq_scale && bandwidth != 0 && frequency != 0)
{
float y_level = inner_bb.Max.y - 8 * ui_scale;
window->DrawList->AddLine({inner_bb.Min.x, y_level}, {inner_bb.Max.x, y_level}, col_base, 3 * ui_scale);
for (int i = 1; i < 10; i++)
{
float x_line = inner_bb.Min.x + (i / 10.0) * res_w;
double freq_here = frequency + ((i / 10.0) * bandwidth) - (bandwidth / 2);
window->DrawList->AddLine({x_line, y_level}, {x_line, y_level - 10 * ui_scale}, col_base, 3);
auto cstr = format_notated(freq_here);
window->DrawList->AddText({x_line - ImGui::CalcTextSize(cstr.c_str()).x / 2, y_level - 30 * ui_scale},
i == 5 ? (ImU32)style::theme.green : col_base,
cstr.c_str());
if (i == 5 && actual_sdr_freq != -1)
{
auto cstr = "(" + format_notated(actual_sdr_freq) + ")";
window->DrawList->AddText({x_line - ImGui::CalcTextSize(cstr.c_str()).x / 2, y_level - 16 * ui_scale},
i == 5 ? (ImU32)style::theme.red : col_base,
cstr.c_str());
}
}
}
// Draw VFOs
if (bandwidth != 0 && frequency != 0 && vfo_freqs.size() > 0)
{
const ImU32 col_base_vfo = style::theme.red;
for (auto vfo : vfo_freqs)
{
double vfreq = vfo.second - frequency;
float y_level = inner_bb.Max.y - 8 * ui_scale;
float x_line = inner_bb.Min.x + (((bandwidth / 2) + vfreq) / bandwidth) * res_w;
window->DrawList->AddLine({x_line, y_level}, {x_line, y_level - 10 * ui_scale}, col_base_vfo, 3);
window->DrawList->AddLine({x_line, y_level - 10 * ui_scale}, {x_line - 5 * ui_scale, y_level - 20 * ui_scale}, col_base_vfo, 3);
window->DrawList->AddLine({x_line, y_level - 10 * ui_scale}, {x_line + 5 * ui_scale, y_level - 20 * ui_scale}, col_base_vfo, 3);
if (sqrtf(powf(ImGui::GetMousePos().x - x_line, 2) + powf(ImGui::GetMousePos().y - y_level, 2)) < 40 * ui_scale)
window->DrawList->AddText({x_line - ImGui::CalcTextSize(vfo.first.c_str()).x / 2, y_level - 45 * ui_scale},
col_base_vfo,
vfo.first.c_str());
}
}
// Draw lines
double fz = (double)values_size / (double)res_w;
for (int n = 0; n < res_w; n++)
{
const float t1 = t0 + t_step;
// const int v1_idx = (int)(t0 * item_count + 0.5f);
float ffpos = n * fz;
if (ffpos >= values_size)
ffpos = values_size - 1;
float finals = -INFINITY;
for (float v = ffpos; v < ffpos + fz; v += 1)
if (finals < values[(int)floor(v)])
finals = values[(int)floor(v)];
const float v1 = finals; // values[v1_idx];
const ImVec2 tp1 = ImVec2(t1, 1.0f - ImSaturate((v1 - scale_min) * inv_scale));
ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0);
ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, tp1);
window->DrawList->AddLine(pos0, pos1, col_base);
t0 = t1;
tp0 = tp1;
}
ImGui::Dummy(size);
work_mutex.unlock();
}
image::Image FFTPlot::drawImg(int size_x, int size_y)
{
work_mutex.lock();
image::Image img(8, size_x, size_y, 3);
image::TextDrawer text_drawer;
text_drawer.init_font(resources::getResourcePath("fonts/font.ttf"));
int res_w = size_x;
const float t_step = 1.0f / (float)res_w;
const float inv_scale = (scale_min == scale_max) ? 0.0f : (1.0f / (scale_max - scale_min));
float t0 = 0.0f;
float v0 = values[0];
ImVec2 tp0 = ImVec2(t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale));
std::vector<double> color_cyan = {0, 237.0 / 255.0, 1};
std::vector<double> color_red = {1, 0, 0};
std::vector<double> color_scale = {0.4, 0.4, 0.4};
// uint8_t color_scale2[] = {int(255 * 0.4), int(255 * 0.4), int(255 * 0.4)};
// Draw lines
float vscale = ((scale_max - scale_min) / scale_resolution);
float step = size_y / scale_resolution;
float value = scale_min;
for (float i = size_y; i >= 0; i -= step)
{
ImVec2 pos0 = {0, i};
ImVec2 pos1 = {(float)(size_x - 1), i};
img.draw_line(pos0.x, pos0.y, pos1.x, pos1.y, color_scale);
value += vscale;
text_drawer.draw_text(img, pos0.x, pos0.y + 2, color_scale, 10, std::string(std::to_string(int(value)) + " dB").c_str());
}
#if 0
// Draw Freq Scale
if (enable_freq_scale && bandwidth != 0 && frequency != 0)
{
float y_level = size_x - 8;
img.draw_line(inner_bb.Min.x, y_level, inner_bb.Max.x, y_level, color_cyan, 3);
for (int i = 1; i < 10; i++)
{
float x_line = inner_bb.Min.x + (i / 10.0) * res_w;
double freq_here = frequency + ((i / 10.0) * bandwidth) - (bandwidth / 2);
img.draw_line(x_line, y_level, x_line, y_level - 10, color_cyan, 3);
auto cstr = format_notated(freq_here);
img.draw_text(x_line - ImGui::CalcTextSize(cstr.c_str()).x / 2, y_level - 30,
i == 5 ? color_scale2 : color_cyan, 10 cstr.c_str());
if (i == 5 && actual_sdr_freq != -1)
{
auto cstr = "(" + format_notated(actual_sdr_freq) + ")";
img.draw_text({x_line - ImGui::CalcTextSize(cstr.c_str()).x / 2, y_level - 16},
i == 5 ? color_scale2 : color_cyan, 10,
cstr.c_str());
}
}
}
#endif
// Draw VFOs
if (bandwidth != 0 && frequency != 0 && vfo_freqs.size() > 0)
{
// const ImU32 col_base_vfo = style::theme.red;
for (auto vfo : vfo_freqs)
{
double vfreq = vfo.second - frequency;
float y_level = size_y - 8 * ui_scale;
float x_line = 0 + (((bandwidth / 2) + vfreq) / bandwidth) * res_w;
// window->DrawList->AddLine({x_line, y_level}, {x_line, y_level - 10 * ui_scale}, col_base_vfo, 3);
// window->DrawList->AddLine({x_line, y_level - 10 * ui_scale}, {x_line - 5 * ui_scale, y_level - 20 * ui_scale}, col_base_vfo, 3);
// window->DrawList->AddLine({x_line, y_level - 10 * ui_scale}, {x_line + 5 * ui_scale, y_level - 20 * ui_scale}, col_base_vfo, 3);
img.draw_line(x_line, y_level, x_line, y_level - 10, color_red);
img.draw_line(x_line, y_level - 10, x_line - 5, y_level - 20, color_red);
img.draw_line(x_line, y_level - 10, x_line + 5, y_level - 20, color_red);
// if (sqrtf(powf(ImGui::GetMousePos().x - x_line, 2) + powf(ImGui::GetMousePos().y - y_level, 2)) < 40 * ui_scale)
// window->DrawList->AddText({x_line - ImGui::CalcTextSize(vfo.first.c_str()).x / 2, y_level - 45 * ui_scale},
// col_base_vfo,
// vfo.first.c_str());
}
}
// Draw lines
double fz = (double)values_size / (double)res_w;
for (int n = 0; n < res_w; n++)
{
const float t1 = t0 + t_step;
// const int v1_idx = (int)(t0 * item_count + 0.5f);
float ffpos = n * fz;
if (ffpos >= values_size)
ffpos = values_size - 1;
float finals = -INFINITY;
for (float v = ffpos; v < ffpos + fz; v += 1)
if (finals < values[(int)floor(v)])
finals = values[(int)floor(v)];
const float v1 = finals; // values[v1_idx];
const ImVec2 tp1 = ImVec2(t1, 1.0f - ImSaturate((v1 - scale_min) * inv_scale));
ImVec2 pos0 = ImLerp({0, 0}, {(float)size_x, (float)size_y}, tp0);
ImVec2 pos1 = ImLerp({0, 0}, {(float)size_x, (float)size_y}, tp1);
img.draw_line(pos0.x, pos0.y, pos1.x, pos1.y, color_cyan);
t0 = t1;
tp0 = tp1;
}
work_mutex.unlock();
return img;
}
}
|