root / gr-fft / lib / goertzel.cc @ 141d2ec0
History | View | Annotate | Download (1.9 kB)
| 1 | 399969c8 | Johnathan Corgan | /* -*- c++ -*- */
|
|---|---|---|---|
| 2 | 399969c8 | Johnathan Corgan | /*
|
| 3 | 399969c8 | Johnathan Corgan | * Copyright 2002,2011,2012 Free Software Foundation, Inc. |
| 4 | 399969c8 | Johnathan Corgan | * |
| 5 | 399969c8 | Johnathan Corgan | * This file is part of GNU Radio |
| 6 | 399969c8 | Johnathan Corgan | * |
| 7 | 399969c8 | Johnathan Corgan | * GNU Radio is free software; you can redistribute it and/or modify |
| 8 | 399969c8 | Johnathan Corgan | * it under the terms of the GNU General Public License as published by |
| 9 | 399969c8 | Johnathan Corgan | * the Free Software Foundation; either version 3, or (at your option) |
| 10 | 399969c8 | Johnathan Corgan | * any later version. |
| 11 | 399969c8 | Johnathan Corgan | * |
| 12 | 399969c8 | Johnathan Corgan | * GNU Radio is distributed in the hope that it will be useful, |
| 13 | 399969c8 | Johnathan Corgan | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | 399969c8 | Johnathan Corgan | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | 399969c8 | Johnathan Corgan | * GNU General Public License for more details. |
| 16 | 399969c8 | Johnathan Corgan | * |
| 17 | 399969c8 | Johnathan Corgan | * You should have received a copy of the GNU General Public License |
| 18 | 399969c8 | Johnathan Corgan | * along with GNU Radio; see the file COPYING. If not, write to |
| 19 | 399969c8 | Johnathan Corgan | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | 399969c8 | Johnathan Corgan | * Boston, MA 02110-1301, USA. |
| 21 | 399969c8 | Johnathan Corgan | */ |
| 22 | 399969c8 | Johnathan Corgan | |
| 23 | 399969c8 | Johnathan Corgan | #ifdef HAVE_CONFIG_H
|
| 24 | 399969c8 | Johnathan Corgan | #include <config.h> |
| 25 | 399969c8 | Johnathan Corgan | #endif
|
| 26 | 399969c8 | Johnathan Corgan | |
| 27 | 399969c8 | Johnathan Corgan | #include <cmath> |
| 28 | 399969c8 | Johnathan Corgan | #include <fft/goertzel.h> |
| 29 | 399969c8 | Johnathan Corgan | |
| 30 | 399969c8 | Johnathan Corgan | namespace gr {
|
| 31 | 399969c8 | Johnathan Corgan | namespace fft {
|
| 32 | 399969c8 | Johnathan Corgan | |
| 33 | 399969c8 | Johnathan Corgan | goertzel::goertzel(int rate, int len, float freq) |
| 34 | 399969c8 | Johnathan Corgan | {
|
| 35 | 399969c8 | Johnathan Corgan | set_params(rate, len, freq); |
| 36 | 399969c8 | Johnathan Corgan | } |
| 37 | 399969c8 | Johnathan Corgan | |
| 38 | 399969c8 | Johnathan Corgan | void
|
| 39 | 399969c8 | Johnathan Corgan | goertzel::set_params(int rate, int len, float freq) |
| 40 | 399969c8 | Johnathan Corgan | {
|
| 41 | 399969c8 | Johnathan Corgan | d_d1 = 0.0; |
| 42 | 399969c8 | Johnathan Corgan | d_d2 = 0.0; |
| 43 | 399969c8 | Johnathan Corgan | |
| 44 | 399969c8 | Johnathan Corgan | float w = 2.0*M_PI*freq/rate; |
| 45 | 399969c8 | Johnathan Corgan | d_wr = 2.0*std::cos(w); |
| 46 | 399969c8 | Johnathan Corgan | d_wi = std::sin(w); |
| 47 | 399969c8 | Johnathan Corgan | d_len = len; |
| 48 | 399969c8 | Johnathan Corgan | d_processed = 0;
|
| 49 | 399969c8 | Johnathan Corgan | } |
| 50 | 399969c8 | Johnathan Corgan | |
| 51 | 399969c8 | Johnathan Corgan | gr_complex |
| 52 | 399969c8 | Johnathan Corgan | goertzel::batch(float *in)
|
| 53 | 399969c8 | Johnathan Corgan | {
|
| 54 | 399969c8 | Johnathan Corgan | d_d1 = 0.0; |
| 55 | 399969c8 | Johnathan Corgan | d_d2 = 0.0; |
| 56 | 399969c8 | Johnathan Corgan | |
| 57 | 399969c8 | Johnathan Corgan | for(int i = 0; i < d_len; i++) |
| 58 | 399969c8 | Johnathan Corgan | input(in[i]); |
| 59 | 399969c8 | Johnathan Corgan | |
| 60 | 399969c8 | Johnathan Corgan | return output();
|
| 61 | 399969c8 | Johnathan Corgan | } |
| 62 | 399969c8 | Johnathan Corgan | |
| 63 | 399969c8 | Johnathan Corgan | void
|
| 64 | 399969c8 | Johnathan Corgan | goertzel::input(const float &input) |
| 65 | 399969c8 | Johnathan Corgan | {
|
| 66 | 399969c8 | Johnathan Corgan | float y = input + d_wr*d_d1 - d_d2;
|
| 67 | 399969c8 | Johnathan Corgan | d_d2 = d_d1; |
| 68 | 399969c8 | Johnathan Corgan | d_d1 = y; |
| 69 | 399969c8 | Johnathan Corgan | d_processed++; |
| 70 | 399969c8 | Johnathan Corgan | } |
| 71 | 399969c8 | Johnathan Corgan | |
| 72 | 399969c8 | Johnathan Corgan | gr_complex |
| 73 | 399969c8 | Johnathan Corgan | goertzel::output() |
| 74 | 399969c8 | Johnathan Corgan | {
|
| 75 | 399969c8 | Johnathan Corgan | gr_complex out((0.5*d_wr*d_d1-d_d2)/d_len, (d_wi*d_d1)/d_len); |
| 76 | 399969c8 | Johnathan Corgan | d_d1 = 0.0; |
| 77 | 399969c8 | Johnathan Corgan | d_d2 = 0.0; |
| 78 | 399969c8 | Johnathan Corgan | d_processed = 0;
|
| 79 | 399969c8 | Johnathan Corgan | return out;
|
| 80 | 399969c8 | Johnathan Corgan | } |
| 81 | 399969c8 | Johnathan Corgan | |
| 82 | 399969c8 | Johnathan Corgan | } /* namespace fft */
|
| 83 | 399969c8 | Johnathan Corgan | }/* namespace gr */
|