2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dsp_demodulate.hpp"
|
|
|
|
|
|
|
|
#include "complex.hpp"
|
|
|
|
#include "fxpt_atan2.hpp"
|
2015-10-15 18:17:40 -04:00
|
|
|
#include "utility_m4.hpp"
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
#include <hal.h>
|
|
|
|
|
|
|
|
namespace dsp {
|
|
|
|
namespace demodulate {
|
|
|
|
|
2016-01-11 19:15:42 -05:00
|
|
|
buffer_f32_t AM::execute(
|
2016-01-04 14:00:17 -05:00
|
|
|
const buffer_c16_t& src,
|
2016-01-11 19:15:42 -05:00
|
|
|
const buffer_f32_t& dst
|
2015-07-08 11:39:24 -04:00
|
|
|
) {
|
2017-04-18 19:49:44 -04:00
|
|
|
const auto src_p = src.p;
|
|
|
|
const auto src_end = &src.p[src.count];
|
2015-07-08 11:39:24 -04:00
|
|
|
auto dst_p = dst.p;
|
|
|
|
while(src_p < src_end) {
|
2017-04-18 19:49:44 -04:00
|
|
|
const uint32_t sample0 = *__SIMD32(src_p)++;
|
|
|
|
const uint32_t sample1 = *__SIMD32(src_p)++;
|
2015-07-08 11:39:24 -04:00
|
|
|
const uint32_t mag_sq0 = __SMUAD(sample0, sample0);
|
|
|
|
const uint32_t mag_sq1 = __SMUAD(sample1, sample1);
|
2016-02-14 15:38:50 -05:00
|
|
|
*(dst_p++) = __builtin_sqrtf(mag_sq0) * k;
|
|
|
|
*(dst_p++) = __builtin_sqrtf(mag_sq1) * k;
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return { dst.p, src.count, src.sampling_rate };
|
|
|
|
}
|
2016-01-30 01:27:18 -05:00
|
|
|
|
|
|
|
buffer_f32_t SSB::execute(
|
|
|
|
const buffer_c16_t& src,
|
|
|
|
const buffer_f32_t& dst
|
|
|
|
) {
|
|
|
|
const complex16_t* src_p = src.p;
|
|
|
|
const auto src_end = &src.p[src.count];
|
|
|
|
auto dst_p = dst.p;
|
|
|
|
while(src_p < src_end) {
|
2016-02-14 15:38:50 -05:00
|
|
|
*(dst_p++) = (src_p++)->real() * k;
|
|
|
|
*(dst_p++) = (src_p++)->real() * k;
|
|
|
|
*(dst_p++) = (src_p++)->real() * k;
|
|
|
|
*(dst_p++) = (src_p++)->real() * k;
|
2016-01-30 01:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return { dst.p, src.count, src.sampling_rate };
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
static inline float angle_approx_4deg0(const complex32_t t) {
|
|
|
|
const auto x = static_cast<float>(t.imag()) / static_cast<float>(t.real());
|
|
|
|
return 16384.0f * x;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
static inline float angle_approx_0deg27(const complex32_t t) {
|
2016-01-11 14:17:33 -05:00
|
|
|
if( t.real() ) {
|
|
|
|
const auto x = static_cast<float>(t.imag()) / static_cast<float>(t.real());
|
|
|
|
return x / (1.0f + 0.28086f * x * x);
|
|
|
|
} else {
|
|
|
|
return (t.imag() < 0) ? -1.5707963268f : 1.5707963268f;
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
2015-12-28 18:50:19 -05:00
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
static inline float angle_precise(const complex32_t t) {
|
|
|
|
return atan2f(t.imag(), t.real());
|
|
|
|
}
|
2015-12-28 18:50:19 -05:00
|
|
|
|
2016-01-11 19:15:42 -05:00
|
|
|
buffer_f32_t FM::execute(
|
|
|
|
const buffer_c16_t& src,
|
|
|
|
const buffer_f32_t& dst
|
|
|
|
) {
|
|
|
|
auto z = z_;
|
|
|
|
|
2017-04-18 19:49:44 -04:00
|
|
|
const auto src_p = src.p;
|
|
|
|
const auto src_end = &src.p[src.count];
|
2016-01-11 19:15:42 -05:00
|
|
|
auto dst_p = dst.p;
|
|
|
|
while(src_p < src_end) {
|
2017-04-18 19:49:44 -04:00
|
|
|
const auto s0 = *__SIMD32(src_p)++;
|
|
|
|
const auto s1 = *__SIMD32(src_p)++;
|
2016-01-11 19:15:42 -05:00
|
|
|
const auto t0 = multiply_conjugate_s16_s32(s0, z);
|
|
|
|
const auto t1 = multiply_conjugate_s16_s32(s1, s0);
|
|
|
|
z = s1;
|
2016-02-14 15:38:50 -05:00
|
|
|
*(dst_p++) = angle_precise(t0) * kf;
|
|
|
|
*(dst_p++) = angle_precise(t1) * kf;
|
2016-01-11 19:15:42 -05:00
|
|
|
}
|
|
|
|
z_ = z;
|
|
|
|
|
|
|
|
return { dst.p, src.count, src.sampling_rate };
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
buffer_s16_t FM::execute(
|
2016-01-04 14:00:17 -05:00
|
|
|
const buffer_c16_t& src,
|
|
|
|
const buffer_s16_t& dst
|
2015-07-08 11:39:24 -04:00
|
|
|
) {
|
|
|
|
auto z = z_;
|
|
|
|
|
2017-04-18 19:49:44 -04:00
|
|
|
const auto src_p = src.p;
|
|
|
|
const auto src_end = &src.p[src.count];
|
2015-07-08 11:39:24 -04:00
|
|
|
auto dst_p = dst.p;
|
|
|
|
while(src_p < src_end) {
|
2017-04-18 19:49:44 -04:00
|
|
|
const auto s0 = *__SIMD32(src_p)++;
|
|
|
|
const auto s1 = *__SIMD32(src_p)++;
|
2015-07-08 11:39:24 -04:00
|
|
|
const auto t0 = multiply_conjugate_s16_s32(s0, z);
|
|
|
|
const auto t1 = multiply_conjugate_s16_s32(s1, s0);
|
|
|
|
z = s1;
|
2016-02-14 15:38:50 -05:00
|
|
|
const int32_t theta0_int = angle_approx_0deg27(t0) * ks16;
|
2015-07-08 11:39:24 -04:00
|
|
|
const int32_t theta0_sat = __SSAT(theta0_int, 16);
|
2016-02-14 15:38:50 -05:00
|
|
|
const int32_t theta1_int = angle_approx_0deg27(t1) * ks16;
|
2015-07-08 11:39:24 -04:00
|
|
|
const int32_t theta1_sat = __SSAT(theta1_int, 16);
|
2017-04-18 19:49:44 -04:00
|
|
|
*__SIMD32(dst_p)++ = __PKHBT(
|
2015-07-08 11:39:24 -04:00
|
|
|
theta0_sat,
|
|
|
|
theta1_sat,
|
|
|
|
16
|
|
|
|
);
|
|
|
|
}
|
|
|
|
z_ = z;
|
|
|
|
|
|
|
|
return { dst.p, src.count, src.sampling_rate };
|
|
|
|
}
|
|
|
|
|
2016-02-04 01:47:44 -05:00
|
|
|
void FM::configure(const float sampling_rate, const float deviation_hz) {
|
|
|
|
/*
|
|
|
|
* angle: -pi to pi. output range: -32768 to 32767.
|
|
|
|
* Maximum delta-theta (output of atan2) at maximum deviation frequency:
|
|
|
|
* delta_theta_max = 2 * pi * deviation / sampling_rate
|
|
|
|
*/
|
2016-02-14 15:38:50 -05:00
|
|
|
kf = static_cast<float>(1.0f / (2.0 * pi * deviation_hz / sampling_rate));
|
|
|
|
ks16 = 32767.0f * kf;
|
2016-02-04 01:47:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
}
|