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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>
//radiola
//#define CONFIG_OS_LINUX
//#include <config.h>
//#include <config/config_linux.h>
#include <draw/glui.h>
#include <hw/hw.h>
#include <hw/sdr.h>
#define SAMPLE_RATE 2048000
#define CENTER_FREQ 446500000
#define FFT_LEVEL 10
#define FFT_SIZE (1 << FFT_LEVEL)
#define SAMPLE_LENGHT (2 * FFT_SIZE)
#define PRESCALE 8
#define POSTSCALE 2
int16_t* Sinewave;
int N_WAVE, LOG2_N_WAVE;
double* power_table;
void helper( char *exec_name )
{
const char help_str[]="Usage: ./%s [OPTIONS]\n\
-f [FREQ] - set center frequency\n\
-s [SAMPLE] - set sample rate\n\
-d [DEVICE] - choose device\n\
-? - print help\n\
";
printf( help_str, exec_name );
}
//better to have size size mod olen == 0
int normalise( uint8_t *ibuf, int ilen, uint8_t *obuf, int olen )
{
int i,j,m;
int ppi;
/*
if ( ilen >= olen )
{
ppi = ilen / olen;
} else {
return -1;
}
*/
ppi = 1;
m = 0;
i = 0;
while ( (i < ilen) && (m < olen) )
{
uint32_t sum=0;
for ( j=0; j<ppi; j++ );
sum += ibuf[i+j];
sum /= ppi;
obuf[m] = sum;
//printf("%d-", obuf[m] );
i += ppi;
m += 1;
}
return 0;
}
void sine_table(int size)
{
int i;
double d;
LOG2_N_WAVE = size;
N_WAVE = 1 << LOG2_N_WAVE;
Sinewave = malloc(sizeof(int16_t) * N_WAVE*3/4);
power_table = malloc(sizeof(double) * N_WAVE);
for (i=0; i<N_WAVE*3/4; i++)
{
d = (double)i * 2.0 * M_PI / N_WAVE;
Sinewave[i] = (int)round(32767*sin(d));
}
}
int16_t FIX_MPY(int16_t a, int16_t b)
/* fixed point multiply and scale */
{
int c = ((int)a * (int)b) >> 14;
b = c & 0x01;
return (c >> 1) + b;
}
int32_t real_conj(int16_t real, int16_t imag)
/* real(n * conj(n)) */
{
return ((int32_t)real*(int32_t)real + (int32_t)imag*(int32_t)imag);
}
int fix_fft(int16_t iq[], int m)
/* interleaved iq[], 0 <= n < 2**m, changes in place */
{
int mr, nn, i, j, l, k, istep, n, shift;
int16_t qr, qi, tr, ti, wr, wi;
n = 1 << m;
if (n > N_WAVE)
{return -1;}
mr = 0;
nn = n - 1;
/* decimation in time - re-order data */
for (m=1; m<=nn; ++m) {
l = n;
do
{l >>= 1;}
while (mr+l > nn);
mr = (mr & (l-1)) + l;
if (mr <= m)
{continue;}
// real = 2*m, imag = 2*m+1
tr = iq[2*m];
iq[2*m] = iq[2*mr];
iq[2*mr] = tr;
ti = iq[2*m+1];
iq[2*m+1] = iq[2*mr+1];
iq[2*mr+1] = ti;
}
l = 1;
k = LOG2_N_WAVE-1;
while (l < n) {
shift = 1;
istep = l << 1;
for (m=0; m<l; ++m) {
j = m << k;
wr = Sinewave[j+N_WAVE/4];
wi = -Sinewave[j];
if (shift) {
wr >>= 1; wi >>= 1;}
for (i=m; i<n; i+=istep) {
j = i + l;
tr = FIX_MPY(wr,iq[2*j]) - FIX_MPY(wi,iq[2*j+1]);
ti = FIX_MPY(wr,iq[2*j+1]) + FIX_MPY(wi,iq[2*j]);
qr = iq[2*i];
qi = iq[2*i+1];
if (shift) {
qr >>= 1; qi >>= 1;}
iq[2*j] = qr - tr;
iq[2*j+1] = qi - ti;
iq[2*i] = qr + tr;
iq[2*i+1] = qi + ti;
}
}
--k;
l = istep;
}
return 0;
}
//fftize
int simple_fft( uint8_t *buf, int len )
{
int i,j;
uint16_t p;
uint16_t buf1[SAMPLE_LENGHT];
uint16_t buf2[SAMPLE_LENGHT];
int fft_len;
for (i=0; i<len; i++)
{
buf1[i] = buf[i] * PRESCALE;
}
fix_fft( (uint16_t *)buf1, FFT_LEVEL );
for (i=0; i<FFT_SIZE; i+=1)
{
//buf1[i] = rtl_out.buf[i];
//p = buf1[i] * buf1[i];
j = i*2;
p = (int16_t)real_conj(buf1[j], buf1[j + 1]);
buf2[i] = p;
}
fft_len = FFT_SIZE / 2;
for (i=0; i<fft_len; i++)
{
buf[i] = (int)log10(POSTSCALE * (float)buf2[i+fft_len]);
buf[i+fft_len] = (int)log10(POSTSCALE * (float)buf2[i]);
}
return 0;
}
int main( int argc, char **argv )
{
int ret;
int i,j;
int c;
uint8_t *buf, *sample_buf;
int buf_len, sample_len;
uint32_t dev_num;
//config params
int config_device = 0;
uint32_t config_freq = CENTER_FREQ;
//int config_gain = 1;
//int config_agc = 1;
uint32_t config_sample_rate = SAMPLE_RATE;
char *endptr = NULL;
sdr_t *sdr = NULL;
dongle_t *dongle = NULL;
glui_t *t = NULL;
glui_waterfall_t *w = NULL;
// get all argument configs
opterr = 0;
while ( (c = getopt(argc, argv, "f:s:d:")) != -1 )
{
switch ( c )
{
case 'f':
config_freq = atoi( optarg );
break;
case 's':
config_sample_rate = atoi( optarg );
break;
case 'd':
//printf(" %s \n", optarg);
config_device = atoi( optarg );
break;
case '?':
helper( argv[0] );
break;
case ':':
printf("\n");
break;
default:
printf("Unknow option\n");
return -1;
}
}
if ( (sdr = sdr_init()) == NULL )
{
printf("Cannot init sdr manager\n");
sdr = NULL;
goto main_exit;
}
if ( sdr_open_device( sdr, config_device ) != 0 )
{
printf("MAIN:Cannot open device %d\n", config_device);
sdr->dongle = NULL;
goto main_exit;
}
dongle = sdr_get_device_id( sdr, config_device );
ret = 0;
ret != dongle_set_freq( dongle, config_freq );
ret != dongle_set_sample_rate( dongle, config_sample_rate );
ret != dongle_set_gain( dongle, 0 );
ret != dongle_set_agc( dongle, 40 );
if (ret != 0)
{
printf("Cannot properly config device\n");
}
sine_table( FFT_LEVEL );
//printf("%x\n",t);
//open GUI
if ( glui_init( &t ) == -1 )
{
printf("Cannot set glui\n");
return 1;
}
//printf("%x\n",t);
if ( glui_waterfall( &t, &w ) == -1 )
{
printf("Cannot set waterfall\n");
return 1;
}
/*
dev_num = rtlsdr_get_device_count();
if ( dev_num < 1 )
{
printf( "Cannot find any device" );
goto main_exit;
}
*/
//screen normilised buffer to draw
buf_len = sizeof(char)*w->w;
buf = malloc( buf_len );
sample_len = SAMPLE_LENGHT;
sample_buf = malloc( sample_len );
srand(0); //fake seed
for ( i=0; i<4000;i++ )
{
//for (j=0; j<buf_len; j++)
// sample_buf[j] = (uint8_t)((rand()&0xff));
//read some samples
dongle_read_samples( dongle, sample_buf, sample_len );
//do fft
simple_fft( sample_buf, sample_len );
//prepare to show on the screen
//if (normalise( sample_buf, sample_len, buf, buf_len ) == -1)
{
//printf("Cannot normalise\n");
}
glui_waterfall_data( t, sample_len/2, sample_buf );
//printf("\n\b");
usleep(10000);
}
main_exit:
//close gui, restore terminal mode
glui_close( t );
sdr_close( sdr );
return 0;
}
|