2016-02-16 17:56:55 +00:00
|
|
|
/* C-Netz audio spectrum inversion (Sprachverschleierung)
|
|
|
|
*
|
|
|
|
* (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <math.h>
|
2017-11-18 07:06:06 +00:00
|
|
|
#include "../libsample/sample.h"
|
2016-02-16 17:56:55 +00:00
|
|
|
#include "scrambler.h"
|
|
|
|
|
|
|
|
#define PI M_PI
|
|
|
|
|
|
|
|
/* FTZ 171 TR 60 Clause 6.2
|
|
|
|
* Carrier frequency, that is the spectrum that is mirrored */
|
|
|
|
#define CARRIER_HZ 3300.0
|
2016-05-05 07:52:09 +00:00
|
|
|
#define FILTER_BELOW 300.0
|
|
|
|
#define FILTER_TURNS 2
|
2016-02-16 17:56:55 +00:00
|
|
|
|
|
|
|
/* FTZ 171 TR 60 Clause 6.3
|
|
|
|
* How much must the carrier frequency be lower than a 1000 HZ tone that passes the inversion.
|
2016-05-05 07:52:09 +00:00
|
|
|
* The filter must be tuned to get that loss. */
|
2016-02-16 17:56:55 +00:00
|
|
|
#define TEST_1000HZ_DB 55.0
|
|
|
|
|
|
|
|
/* sine wave for carrier to modulate to */
|
2017-01-29 06:25:12 +00:00
|
|
|
static double carrier[65536];
|
2016-02-16 17:56:55 +00:00
|
|
|
|
|
|
|
void scrambler_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-01-29 06:25:12 +00:00
|
|
|
for (i = 0; i < 65536; i++) {
|
2017-01-27 15:57:34 +00:00
|
|
|
/* our amplitude must be doubled, since we have one spectrum above and one below carrier */
|
2017-01-29 06:25:12 +00:00
|
|
|
carrier[i] = sin((double)i / 65536.0 * 2 * PI) * 2.0;
|
2016-02-16 17:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void scrambler_setup(scrambler_t *scrambler, int samplerate)
|
|
|
|
{
|
2017-05-13 14:04:00 +00:00
|
|
|
iir_lowpass_init(&scrambler->lp, CARRIER_HZ - FILTER_BELOW, samplerate, FILTER_TURNS);
|
2017-01-29 06:25:12 +00:00
|
|
|
scrambler->carrier_phaseshift65536 = 65536.0 / ((double)samplerate / CARRIER_HZ);
|
2016-02-16 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Modulate samples to carriere that is twice the mirror frequency.
|
|
|
|
* Then we got spectrum above carrier and mirrored spectrum below carrier.
|
|
|
|
* Afterwards we cut off carrier frequency and frequencies above carrier.
|
|
|
|
*/
|
2017-01-27 15:57:34 +00:00
|
|
|
void scrambler(scrambler_t *scrambler, sample_t *samples, int length)
|
2016-02-16 17:56:55 +00:00
|
|
|
{
|
|
|
|
double phaseshift, phase;
|
|
|
|
int i;
|
|
|
|
|
2017-01-29 06:25:12 +00:00
|
|
|
phaseshift = scrambler->carrier_phaseshift65536;
|
|
|
|
phase = scrambler->carrier_phase65536;
|
2016-02-16 17:56:55 +00:00
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
/* modulate samples to carrier */
|
2017-01-29 06:25:12 +00:00
|
|
|
samples[i] *= carrier[(uint16_t)phase];
|
2016-02-16 17:56:55 +00:00
|
|
|
phase += phaseshift;
|
2017-01-29 06:25:12 +00:00
|
|
|
if (phase >= 65536.0)
|
|
|
|
phase -= 65536.0;
|
2016-02-16 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 06:25:12 +00:00
|
|
|
scrambler->carrier_phase65536 = phase;
|
2016-02-16 17:56:55 +00:00
|
|
|
|
|
|
|
/* cut off carrier frequency and modulation above carrier frequency */
|
2017-05-13 14:04:00 +00:00
|
|
|
iir_process(&scrambler->lp, samples, length);
|
2016-02-16 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|