Skip to content

Commit

Permalink
* Add home/end to skip between 2 and 5ghz
Browse files Browse the repository at this point in the history
* Add a history buffer to the current readings, so i can get a better idea
  of what's going on graphically (and yes it's pretty)
* change the default alpha for the max to not be 255 ,so there's some
  .. prettiness too.
  • Loading branch information
adrian authored and adrian committed Jan 8, 2013
1 parent 7755346 commit d35a685
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
54 changes: 43 additions & 11 deletions src/spectral_fft/fft_eval.c
Expand Up @@ -130,17 +130,26 @@ int pixel(Uint32 *pixels, int x, int y, Uint32 color)
return 0;
}


#define SIZE 2

/* Is this pixel in the viewport? */
static int
is_in_viewport(int x, int y)
{
if (x - SIZE < 0 || x + SIZE >= WIDTH)
return 0;
if (y - SIZE < 0 || y + SIZE >= HEIGHT)
return 0;
return (1);
}

/* this function blends a 2*SIZE x 2*SIZE blob at the given position with
* the defined opacity. */
int bigpixel(Uint32 *pixels, int x, int y, Uint32 color, uint8_t opacity)
{
int x1, y1;

if (x - SIZE < 0 || x + SIZE >= WIDTH)
return -1;
if (y - SIZE < 0 || y + SIZE >= HEIGHT)
if (! is_in_viewport(x, y))
return -1;

for (x1 = x - SIZE; x1 < x + SIZE; x1++)
Expand Down Expand Up @@ -188,7 +197,7 @@ int render_text(SDL_Surface *surface, char *text, int x, int y)
int draw_picture(int highlight, int startfreq)
{
Uint32 *pixels, color, opacity;
int x, y, i, rnum;
int x, y, i, rnum, j;
int highlight_freq = startfreq + 20;
char text[1024];
struct scanresult *result;
Expand Down Expand Up @@ -227,24 +236,38 @@ int draw_picture(int highlight, int startfreq)

/* Render 2300 -> 6000 in 1MHz increments, but using KHz math */
/* .. as right now the canvas is .. quite large. */
/* XXX should just do it based on the current viewport! */
for (i = 2300*1000; i < 6000*1000; i+= 250) {
float signal;
int freqKhz = i;
int16_t *s;

x = X_SCALE * (freqKhz - (startfreq * 1000)) / 1000;

if (x < 0 || x > WIDTH)
continue;

/* Fetch dBm value at the given frequency in KHz */
signal = (float) fft_fetch_freq_avg(freqKhz);
color = BMASK | AMASK;
opacity = 64;
y = 400 - (400.0 + Y_SCALE * signal);
if (bigpixel(pixels, x, y, color, opacity) < 0)
s = fft_fetch_freq_avg(freqKhz);
if (s == NULL)
continue;

for (j = 0; j < FFT_HISTOGRAM_HISTORY_DEPTH; j++) {
if (s[j] == 0)
continue;
signal = (float) s[j];
color = BMASK | AMASK;
opacity = 64;
y = 400 - (400.0 + Y_SCALE * signal);
if (bigpixel(pixels, x, y, color, opacity) < 0)
continue;
}


/* .. and the max */
signal = (float) fft_fetch_freq_max(freqKhz);
color = RMASK | AMASK;
opacity = 255;
opacity = 128;
y = 400 - (400.0 + Y_SCALE * signal);
if (bigpixel(pixels, x, y, color, opacity) < 0)
continue;
Expand Down Expand Up @@ -337,6 +360,15 @@ void graphics_main(void)
case SDLK_PAGEDOWN:
accel+= 2;
scroll = 1;
break;
case SDLK_HOME:
startfreq = 2300;
accel = 0;
break;
case SDLK_END:
startfreq = 5100;
accel = 0;
break;
default:
break;
}
Expand Down
16 changes: 12 additions & 4 deletions src/spectral_fft/fft_histogram.c
Expand Up @@ -58,6 +58,7 @@ fft_add_sample(struct radar_entry *re, struct radar_fft_entry *fe)
float ffreq;
int i;
int fidx;
int cur;

for (i = 0; i < SPECTRAL_HT20_NUM_BINS; i++) {
/* Calculate frequency of the given event */
Expand All @@ -74,7 +75,13 @@ fft_add_sample(struct radar_entry *re, struct radar_fft_entry *fe)
continue;

/* Rolling/decaying average */
fdata.avg_pts[fidx] = (((fdata.avg_pts[fidx] * 100) / 90) + fe->pri.bins[i].dBm) / 2;
cur = fdata.avg_pts_cur[fidx];
if (fdata.avg_pts[fidx][cur] == 0) {
fdata.avg_pts[fidx][cur] = fe->pri.bins[i].dBm;
} else {
fdata.avg_pts[fidx][cur] = (((fdata.avg_pts[fidx][cur] * 100) / 90) + fe->pri.bins[i].dBm) / 2;
}
fdata.avg_pts_cur[fidx] = (fdata.avg_pts_cur[fidx] + 1) % FFT_HISTOGRAM_HISTORY_DEPTH;

/* Max */
if (fdata.max_pts[fidx] == 0 || fe->pri.bins[i].dBm > fdata.max_pts[fidx]) {
Expand All @@ -85,18 +92,19 @@ fft_add_sample(struct radar_entry *re, struct radar_fft_entry *fe)
}
}

int
int16_t *
fft_fetch_freq_avg(int freqKhz)
{
int fidx;

fidx = freq2fidx(freqKhz);
if (fidx < 0)
return -180; /* XXX */
return NULL;

return fdata.avg_pts[fidx];
}
int

int16_t
fft_fetch_freq_max(int freqKhz)
{
int fidx;
Expand Down
12 changes: 8 additions & 4 deletions src/spectral_fft/fft_histogram.h
Expand Up @@ -8,16 +8,20 @@

#define FFT_HISTOGRAM_SIZE \
((6000-2300)*FFT_HISTOGRAM_RESOLUTION)
#define FFT_HISTOGRAM_HISTORY_DEPTH 10

struct fft_histogram_data {
int avg_pts[FFT_HISTOGRAM_SIZE];
int max_pts[FFT_HISTOGRAM_SIZE];
/* XXX should struct-ize these! */
int16_t avg_pts[FFT_HISTOGRAM_SIZE][FFT_HISTOGRAM_HISTORY_DEPTH];
int16_t avg_pts_cur[FFT_HISTOGRAM_SIZE];

int16_t max_pts[FFT_HISTOGRAM_SIZE];
};

extern void fft_histogram_init(void);
extern void fft_add_sample(struct radar_entry *re,
struct radar_fft_entry *fe);
extern int fft_fetch_freq_avg(int freqKhz);
extern int fft_fetch_freq_max(int freqKhz);
extern int16_t * fft_fetch_freq_avg(int freqKhz);
extern int16_t fft_fetch_freq_max(int freqKhz);

#endif

0 comments on commit d35a685

Please sign in to comment.