From 8b9c98a27187324a75a0c429002d5696c3c1579b Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Sun, 30 Nov 2014 14:10:49 +0100 Subject: [PATCH] moved phasor code to phasor.rsh --- app/src/main/rs/decoder.rs | 32 +++++++---------------------- app/src/main/rs/phasor.rsh | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 app/src/main/rs/phasor.rsh diff --git a/app/src/main/rs/decoder.rs b/app/src/main/rs/decoder.rs index b167615..343a1ee 100644 --- a/app/src/main/rs/decoder.rs +++ b/app/src/main/rs/decoder.rs @@ -19,6 +19,7 @@ limitations under the License. #include "complex.rsh" #include "ema.rsh" +#include "phasor.rsh" short *audio_buffer; uchar *value_buffer; @@ -106,32 +107,16 @@ static complex_t dat_lowpass(complex_t input) return cema_cascade(output, input, ema_dat_a, filter_order); } -static complex_t cnt_phasor, cnt_phasor_delta; -static complex_t cnt_phasor_rotate() -{ - complex_t prev = cnt_phasor; - cnt_phasor = cmul(cnt_phasor, cnt_phasor_delta); - cnt_phasor = normalize(cnt_phasor); - return prev; -} - -static complex_t dat_phasor, dat_phasor_delta; -static complex_t dat_phasor_rotate() -{ - complex_t prev = dat_phasor; - dat_phasor = cmul(dat_phasor, dat_phasor_delta); - dat_phasor = normalize(dat_phasor); - return prev; -} - +static phasor_t cnt_phasor; static complex_t cnt_ddc(float amp) { - return cnt_lowpass(amp * cnt_phasor_rotate()); + return cnt_lowpass(amp * rotate(&cnt_phasor)); } +static phasor_t dat_phasor; static complex_t dat_ddc(float amp) { - return dat_lowpass(amp * dat_phasor_rotate()); + return dat_lowpass(amp * rotate(&dat_phasor)); } static float cnt_fmd_scale; @@ -444,11 +429,8 @@ void initialize(float rate, int length, int width, int height) ema_cnt_a = ema_cascade_a(cnt_bandwidth, sample_rate, filter_order); ema_dat_a = ema_cascade_a(dat_bandwidth, sample_rate, filter_order); - cnt_phasor = complex(0.0f, 1.0f); - cnt_phasor_delta = cexp(complex(0.0f, -2.0f * M_PI * cnt_carrier / sample_rate)); - - dat_phasor = complex(0.0f, 1.0f); - dat_phasor_delta = cexp(complex(0.0f, -2.0f * M_PI * dat_carrier / sample_rate)); + cnt_phasor = phasor(-cnt_carrier, sample_rate); + dat_phasor = phasor(-dat_carrier, sample_rate); cnt_fmd_scale = sample_rate / (M_PI * cnt_bandwidth); dat_fmd_scale = sample_rate / (M_PI * dat_bandwidth); diff --git a/app/src/main/rs/phasor.rsh b/app/src/main/rs/phasor.rsh new file mode 100644 index 0000000..6262fe2 --- /dev/null +++ b/app/src/main/rs/phasor.rsh @@ -0,0 +1,41 @@ +/* +Copyright 2014 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 PHASOR_RSH +#define PHASOR_RSH + +#include "complex.rsh" + +typedef struct { + complex_t phasor, delta; +} phasor_t; + +static phasor_t phasor(float freq, float rate) +{ + return (phasor_t){ + complex(0.0f, 1.0f), + cexp(complex(0.0f, 2.0f * M_PI * freq / rate)) + }; +} + +static inline complex_t rotate(phasor_t *phasor) +{ + complex_t prev = phasor->phasor; + phasor->phasor = normalize(cmul(phasor->phasor, phasor->delta)); + return prev; +} + +#endif \ No newline at end of file