From 9dcbdbdc69b8e7e01c4ca8876346e9d54ccc6135 Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Thu, 8 Jan 2015 22:18:50 +0100 Subject: [PATCH] use cic filter for decimation in analyzer --- app/src/main/rs/cic.rsh | 55 ++++++++++++++++++++++++++++++++++++++++ app/src/main/rs/stft.rsh | 12 +++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 app/src/main/rs/cic.rsh diff --git a/app/src/main/rs/cic.rsh b/app/src/main/rs/cic.rsh new file mode 100644 index 0000000..5c8ab79 --- /dev/null +++ b/app/src/main/rs/cic.rsh @@ -0,0 +1,55 @@ +/* +Copyright 2015 Ahmet Inan + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + */ + +#ifndef CIC_RSH +#define CIC_RSH + +typedef struct { + int prev_comb, prev_int; +} cic_t; + +static const int cic_cascade_order = 5; +typedef struct { + cic_t cic[cic_cascade_order]; +} cic_cascade_t; + +static int cic_comb(cic_t *cic, int input) +{ + int output = input - cic->prev_comb; + cic->prev_comb = input; + return output; +} + +static int cic_int(cic_t *cic, int input) +{ + return cic->prev_int = input + cic->prev_int; +} + +static int cic_comb_cascade(cic_cascade_t *cascade, int input) +{ + for (int i = 0; i < cic_cascade_order; ++i) + input = cic_comb(cascade->cic + i, input); + return input; +} + +static int cic_int_cascade(cic_cascade_t *cascade, int input) +{ + for (int i = 0; i < cic_cascade_order; ++i) + input = cic_int(cascade->cic + i, input); + return input; +} + +#endif \ No newline at end of file diff --git a/app/src/main/rs/stft.rsh b/app/src/main/rs/stft.rsh index 831f12d..f8ed371 100644 --- a/app/src/main/rs/stft.rsh +++ b/app/src/main/rs/stft.rsh @@ -22,6 +22,7 @@ limitations under the License. #include "utils.rsh" #include "complex.rsh" #include "radix2.rsh" +#include "cic.rsh" #include "stft_generated.rsh" static inline uchar4 rainbow(float v) @@ -43,11 +44,18 @@ static void freq_marker(int freq) static void spectrum_analyzer(int amplitude) { const int M = 7; - static int n; + static int n, m; static complex_t input[radix2_N]; static complex_t output[radix2_N]; + static cic_cascade_t cascade; - input[(n/M)&(radix2_N-1)] += complex(stft_w[n] * amplitude, 0.0f); + int tmp = cic_int_cascade(&cascade, amplitude); + if (++m < M) + return; + m = 0; + amplitude = cic_comb_cascade(&cascade, tmp); + + input[n&(radix2_N-1)] += complex(stft_w[n] * amplitude, 0.0f); if (++n >= stft_N) { n = 0; // yep, were wasting 3x performance