3 This file is part of a program that implements a Software-Defined Radio.
5 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 The authors can be reached by email at
29 The DTTS Microwave Society
36 // snapshot of current signal
38 snap_spectrum(SpecBlock *sb, int label) {
41 // where most recent signal started
42 j = (sb->fill - sb->buflen + sb->size) % sb->size;
44 // copy starting from there in circular fashion,
45 // applying window as we go
46 for (i = 0; i < sb->size; i++) {
47 CXBdata(sb->timebuf, i) = Cscl(CXBdata(sb->timebuf, j), sb->window[i]);
54 // snapshot -> frequency domain
56 compute_spectrum(SpecBlock *sb) {
57 int i, half = sb->size / 2;
59 // assume timebuf has windowed current snapshot
62 (fftw_complex *) CXBbase(sb->timebuf),
63 (fftw_complex *) CXBbase(sb->freqbuf));
65 if (sb->scale == SPEC_MAG) {
66 for (i = 0; i < half; i++)
67 sb->output[i + half] = Cmag(CXBdata(sb->freqbuf, i));
68 for (; i < sb->size; i++)
69 sb->output[i] = Cmag(CXBdata(sb->freqbuf, i));
72 for (i = 0; i < half; i++)
73 sb->output[i + half] = 10.0 * log10(Csqrmag(CXBdata(sb->freqbuf, i)) + 1e-60);
74 for (; i < sb->size; i++)
75 sb->output[i] = 10.0 * log10(Csqrmag(CXBdata(sb->freqbuf, i)) + 1e-60);
80 init_spectrum(SpecBlock *sb) {
81 sb->fill = sb->size - sb->buflen;
82 sb->accum = newCXB(sb->size, 0, "spectrum accum");
83 sb->timebuf = newCXB(sb->size, 0, "spectrum timebuf");
84 sb->freqbuf = newCXB(sb->size, 0, "spectrum freqbuf");
85 sb->window = newvec_REAL(sb->size, "spectrum window");
86 sb->output = (float *) safealloc(sb->size, sizeof(float), "spectrum output");
87 sb->plan = fftw_create_plan(sb->size, FFTW_FORWARD, sb->planbits);
91 reinit_spectrum(SpecBlock *sb) {
92 sb->fill = sb->size - sb->buflen;
93 memset((char *) CXBbase(sb->accum), 0, sb->size * sizeof(REAL));
94 memset((char *) sb->output, 0, sb->size * sizeof(float));
98 finish_spectrum(SpecBlock *sb) {
103 delvec_REAL(sb->window);
104 safefree((char *) sb->output);
105 fftw_destroy_plan(sb->plan);