-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft_hist.cpp
executable file
·395 lines (348 loc) · 9.65 KB
/
fft_hist.cpp
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "fft_hist.h"
#include "string.h"
#include "memory.h"
#include <time.h>
#include <stdio.h>
#include <iostream> // library that contain basic input/output functions
#include <fstream> // library that contains file input/output functions
using namespace std;
FFT_Hist::FFT_Hist()
{
m_alldat = NULL;
m_avg = NULL;
m_noise_floor = NULL;
m_maxvalues = NULL;
m_minvalues = NULL;
m_timestamp = NULL;
//m_numrows = 0;
m_binsize = 0;
m_avg_min = 0;
m_avg_max = 0;
m_CFHz = 0;
m_SPS = 0;
m_emva_alpha = .13;//
Reset(DEFAULT_BIN_SIZE);
m_binsize = DEFAULT_BIN_SIZE;
m_noise_floor_binwidth = DEFAULT_NOISE_FLOOR_BINS;
m_noise_floor_offset = 0;//DEF_NOISE_FLOOR_OFFSET;
m_curidx = -1;
m_rowcnt = 0;
}
FFT_Hist::~FFT_Hist()
{
Release();
}
void FFT_Hist::Reset(int binsize)
{
if(m_binsize == binsize)
{
ClearMinMaxValues();
for(int c = 0; c < binsize ; c++)
m_avg[c] = NOISE_FLOOR;
m_curidx = -1; // reset back to the first row
return; // already the right size
}
Release(); // free all previously allocated memory
// printf("FFT_Helper::Reset Binsize = %d\r\n",binsize);
m_binsize = binsize; //the X size of the bins
//allocate memory
m_maxvalues = new float[(unsigned)m_binsize]; // max values is a single row
m_minvalues = new float[(unsigned)m_binsize]; // min values is a single row
m_alldat = new float[(unsigned)m_binsize * MAX_FFT_ROWS];
m_avg = new float[(unsigned)m_binsize];
m_noise_floor = new float[(unsigned)m_binsize];
m_timestamp = new long long[MAX_FFT_ROWS];
ClearMinMaxValues();
for(int c = 0; c < binsize ; c++)
m_avg[c] = NOISE_FLOOR;
m_curidx = -1; // reset back to the first row
m_rowcnt = 0;
}
void FFT_Hist::Release() // release allocated memory
{
if(m_alldat != NULL )
{
delete []m_alldat;
m_alldat = NULL;
}
if(m_avg != NULL )
{
delete []m_avg;
m_avg = NULL;
}
if(m_noise_floor != NULL )
{
delete []m_noise_floor;
m_noise_floor = NULL;
}
if(m_maxvalues != NULL )
{
delete []m_maxvalues;
m_maxvalues = NULL;
}
if(m_minvalues != NULL )
{
delete []m_minvalues;
m_minvalues = NULL;
}
if(m_timestamp != NULL)
{
delete []m_timestamp;
m_timestamp = NULL;
}
}
int FFT_Hist::GetBinSize()
{
return m_binsize;
}
void FFT_Hist::ClearMinMaxValues()
{
for(int c=0 ; c < m_binsize ; c++)
{
m_maxvalues[c] = NOISE_FLOOR;// set to noise floor
m_minvalues[c] = 0;
}
}
float FFT_Hist::GetMinDBM(bool unbounded)
{
if(unbounded)
return m_avg_min;
//find the nearest 10 below this
if(m_avg_min < NOISE_FLOOR)
{
return NOISE_FLOOR; // cap it at -130
}else{
return m_avg_min;
}
}
float FFT_Hist::GetMaxDBM()
{
return m_avg_max;
}
void FFT_Hist::AddData(float *fft, int numbins, float centerfreq,float SPS, long long time_uSecond)
{
Lock();
if(numbins != m_binsize) // support a changing bin size if needed
{
Reset(numbins);
}
if(centerfreq != m_CFHz || m_SPS != SPS)
{
m_CFHz = centerfreq; // set the new CF
m_SPS = SPS;
ClearMinMaxValues();
m_avg_min =fft[0]; // get the first entry as a starting point
m_avg_max =fft[0];
}
//increment the m_curidx
m_curidx++;
if(m_curidx == MAX_FFT_ROWS)
m_curidx = -1; // roll over
// cout << m_curidx << "\r\n";
// printf("curidx %d\r\n",m_curidx);
int rowsz = m_binsize * sizeof(float); // size of 1 row in bytes
//copy the fft data into the specified row
memcpy(&m_alldat[m_binsize * m_curidx],fft,rowsz);
if(m_rowcnt == 0) //if this is the fist row, copy it over to the average
{
memcpy(m_avg,fft,rowsz);
}
CalcAvgEMA(fft); // calculate the exponential moving average
CalcMinMax(fft); // look for the high-water marks on the instantaneous FFT values
m_rowcnt++;
if(m_rowcnt > MAX_FFT_ROWS)
m_rowcnt = MAX_FFT_ROWS;
Unlock();
}
void FFT_Hist::CopyTo(float * dest) // copy the entire m_alldat to the specified dest in reverse order for the waterfall
{
if(m_curidx == -1)
return;
//let's do this row by row..
int sourcerow = m_curidx;
//if(sourcerow == 256)
// return;
for(int c = 0 ; c < MAX_FFT_ROWS; c++)
{
float * src;
src = &m_alldat[sourcerow * m_binsize];
memcpy(&dest[c * m_binsize],src,m_binsize * sizeof(float));
sourcerow --;
if(sourcerow < 0)
sourcerow = MAX_FFT_ROWS;
}
}
/*
The idea is that the high-level will be marked, then fade over time
until it goes back to the current level
*/
void FFT_Hist::CalcMinMax(float *vals)
{
for(int x = 0; x < m_binsize;x++)
{
// examine latest row of data
if(vals[x] > m_maxvalues[x])
{
m_maxvalues[x] = vals[x];
}
if(vals[x] < m_minvalues[x])
{
m_minvalues[x] = vals[x];
}
}
}
/*
need a new function to calculate the exponential moving average
https://stackoverflow.com/questions/10990618/calculate-rolling-moving-average-in-c
You pick a constant "alpha" that is between 0 and 1, and compute this:
accumulator = (alpha * new_value) + (1.0 - alpha) * accumulator
You just need to find a value of "alpha" where the effect of a given sample only lasts for about 1000 samples.
*/
void FFT_Hist::CalcAvgEMA(float *linedat)
{
for(int x = 0; x < m_binsize;x++)
{
//accumulator = (alpha * new_value) + (1.0 - alpha) * accumulator
m_avg[x] = ((m_emva_alpha * linedat[x]) + (1.0 - m_emva_alpha) * m_avg[x]); //initialize the value to 0
if(x ==0 )
{
m_avg_min = m_avg[x];
m_avg_max = m_avg[x];
}
if(m_avg[x] < m_avg_min) m_avg_min = m_avg[x];
if(m_avg[x] > m_avg_max) m_avg_max = m_avg[x];
}
}
double FFT_Hist::noise_floor_offset() const
{
return m_noise_floor_offset;
}
void FFT_Hist::setNoise_floor_offset(double noise_floor_offset)
{
m_noise_floor_offset = noise_floor_offset;
}
int FFT_Hist::noise_floor_binwidth() const
{
return m_noise_floor_binwidth;
}
void FFT_Hist::setNoise_floor_binwidth(int noise_floor_binwidth)
{
m_noise_floor_binwidth = noise_floor_binwidth;
}
float *FFT_Hist::MaxValues()
{
return m_maxvalues;
}
float *FFT_Hist::MinValues()
{
return m_minvalues;
}
float FFT_Hist::GetLowFreqHz()
{
float val = m_CFHz;
val -= (m_SPS / 2.0f);
return val;
}
float FFT_Hist::GetHighFreqHz()
{
float val = m_CFHz;
val += (m_SPS / 2.0f);
return val;
}
// return freq in hz from 0 -> DEFAULT_BIN_SIZE-1 value
float FFT_Hist::GetFreqHz(int binidx) // in Hz
{
float leftfreq = GetLowFreqHz();
float freqinc = ((float)m_SPS) / ((float)GetBinSize());
return leftfreq + (binidx * freqinc);
}
// return the closest bin index to the frequency or -1
int FFT_Hist::GetBinIndex(float freqHz)
{
//long leftfreq = m_centerfreq - (m_spanHz / 2);
//long rightfreq = m_centerfreq + (m_spanHz / 2);
float freqinc = m_SPS / (float)GetBinSize();
int binidx = 0;
if(freqHz >= GetLowFreqHz() && freqHz <= GetHighFreqHz())
{
//can I calculate the index directly?
float diff_freq = freqHz - GetLowFreqHz();
binidx = (int)(diff_freq / freqinc);
return binidx;
}else
{
return -1;
}
}
int FFT_Hist::MaxRows()
{
return MAX_FFT_ROWS;
}
float *FFT_Hist::GetAvgRow() // get specified row of data (waterfall) average data
{
return m_avg;
}
/*
calculate the noise floor from the average of the bins
*/
void FFT_Hist::CalcNoiseFloor(float *stddev)
{
int binwidth = m_noise_floor_binwidth;
float sum_of_sq = 0;
for(int c = 0; c < GetBinSize(); c++ )
{
sum_of_sq = 0;
int lowidx = c - binwidth;
int highidx = c + binwidth;
float mean = 0;
if(lowidx < 0 )lowidx = 0;
if(highidx > GetBinSize())
highidx = GetBinSize();
int range = highidx - lowidx;
for (int i = lowidx; i < highidx; i++)
{
mean += m_avg[i];
sum_of_sq += fabs(m_avg[i]) * fabs(m_avg[i]);
}
mean /= range;
if(stddev != nullptr)
{
stddev[c] = sqrt( sum_of_sq / range - mean*mean );
}
m_noise_floor[c] = mean + m_noise_floor_offset;
}
}
QMap<int,float> FFT_Hist::DetectPeaks(float det_thresh_scaler)
{
//det_thresh_scaler = 3.25f; // a multiple of the standard deviation is the threshold point
QMap<int,float> peaks; // the detected peaks
int numbins = GetBinSize();
float tdat[numbins];
float *data = AvgData();
float mean = 0;
float stdev = 0;
//storage for standard deviation
float stddev[numbins];
CalcNoiseFloor(stddev); // calc noise floor and standard deviation
int lastPeak = -1;
// printf("DetectPeaks mean:%f , sumofsq:%f , stddev:%f\r\n",mean,sum_of_sq, stdev );
//get the abs of the average values
for(int c = 0; c < GetBinSize(); c++)
{
tdat[c] = fabs(data[c]);
}
for (int bin = 0; bin < numbins; bin++)
{
mean = fabs(m_noise_floor[bin]);
//det_thresh_scaler times the std over the mean or better than current peak
float d = (lastPeak == -1) ? (mean - det_thresh_scaler * stdev) : tdat[lastPeak];
if (tdat[bin] < d)
lastPeak = bin;
if (lastPeak != -1 && (bin - lastPeak > PEAK_H_TOLERANCE || bin == numbins - 1))
{
peaks.insert(lastPeak, tdat[lastPeak]);
lastPeak = -1;
}
}
return peaks;
}