direwolf/dsp.c
WB2OSZ 8b81a3226f Version 1.2
new file:   A-Better-APRS-Packet-Demodulator-Part-1-1200-baud.pdf
	new file:   A-Better-APRS-Packet-Demodulator-Part-2-9600-baud.pdf
	modified:   APRStt-Implementation-Notes.pdf
	modified:   CHANGES.txt
	new file:   Makefile
	modified:   Makefile.linux
	modified:   Makefile.win
	deleted:    Quick-Start-Guide-Windows.pdf
	modified:   Raspberry-Pi-APRS-Tracker.pdf
	modified:   Raspberry-Pi-APRS.pdf
	modified:   User-Guide.pdf
	modified:   aclients.c
	modified:   aprs_tt.c
	modified:   aprs_tt.h
	modified:   atest.c
	modified:   audio.c
	modified:   audio.h
	modified:   audio_win.c
	modified:   ax25_pad.c
	modified:   ax25_pad.h
	modified:   beacon.c
	modified:   beacon.h
	modified:   config.c
	modified:   config.h
	modified:   decode_aprs.c
	modified:   decode_aprs.h
	modified:   demod.c
	modified:   demod.h
	modified:   demod_9600.c
	modified:   demod_9600.h
	modified:   demod_afsk.c
	modified:   digipeater.c
	modified:   digipeater.h
	modified:   direwolf.c
	modified:   direwolf.conf
	deleted:    direwolf.desktop
	modified:   direwolf.h
	new file:   direwolf.spec
	new file:   direwolf.txt
	new file:   dlq.c
	new file:   dlq.h
	modified:   dsp.c
	new file:   dtime_now.c
	new file:   dtime_now.h
	modified:   dtmf.c
	modified:   dtmf.h
	new file:   dwespeak.bat
	new file:   dwespeak.sh
	modified:   fsk_demod_state.h
	modified:   gen_packets.c
	modified:   gen_tone.c
	new file:   geotranz/README-FIRST.txt
	new file:   geotranz/error_string.c
	new file:   geotranz/error_string.h
	new file:   geotranz/mgrs.c
	new file:   geotranz/mgrs.h
	new file:   geotranz/polarst.c
	new file:   geotranz/polarst.h
	new file:   geotranz/readme.txt
	new file:   geotranz/releasenotes.txt
	new file:   geotranz/tranmerc.c
	new file:   geotranz/tranmerc.h
	new file:   geotranz/ups.c
	new file:   geotranz/ups.h
	new file:   geotranz/usng.c
	new file:   geotranz/usng.h
	new file:   geotranz/utm.c
	new file:   geotranz/utm.h
	modified:   hdlc_rec.c
	modified:   hdlc_rec.h
	modified:   hdlc_rec2.c
	modified:   hdlc_rec2.h
	modified:   hdlc_send.c
	modified:   igate.c
	modified:   igate.h
	modified:   kiss_frame.c
	modified:   kiss_frame.h
	modified:   kissnet.c
	modified:   latlong.c
	modified:   latlong.h
	modified:   ll2utm.c
	modified:   log.c
	modified:   log.h
	new file:   man1/aclients.1
	new file:   man1/atest.1
	new file:   man1/decode_aprs.1
	new file:   man1/direwolf.1
	new file:   man1/gen_packets.1
	new file:   man1/ll2utm.1
	new file:   man1/log2gpx.1
	new file:   man1/text2tt.1
	new file:   man1/tt2text.1
	new file:   man1/utm2ll.1
	modified:   multi_modem.c
	modified:   multi_modem.h
	new file:   pfilter.c
	new file:   pfilter.h
	modified:   ptt.c
	modified:   ptt.h
	deleted:    pttest.c
	modified:   rdq.c
	new file:   recv.c
	new file:   recv.h
	modified:   redecode.c
	modified:   redecode.h
	modified:   rrbb.c
	modified:   rrbb.h
	modified:   server.c
	modified:   server.h
	modified:   telemetry.c
	modified:   telemetry.h
	modified:   textcolor.c
	modified:   textcolor.h
	modified:   tocalls.txt
	modified:   tq.c
	modified:   tq.h
	modified:   tt_text.c
	modified:   tt_text.h
	modified:   tt_user.c
	modified:   tt_user.h
	new file:   ttcalc.c
	deleted:    udp_test.c
	deleted:    utm/LatLong-UTMconversion.c
	deleted:    utm/LatLong-UTMconversion.h
	deleted:    utm/README.txt
	deleted:    utm/SwissGrid.cpp
	deleted:    utm/UTMConversions.cpp
	deleted:    utm/constants.h
	modified:   utm2ll.c
	modified:   version.h
	new file:   xid.c
	modified:   xmit.c
	modified:   xmit.h
2015-07-26 21:17:23 -04:00

253 lines
6.1 KiB
C

//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011, 2012, 2013, 2015 John Langner, WB2OSZ
//
// 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 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/>.
//
/*------------------------------------------------------------------
*
* Name: dsp.c
*
* Purpose: Generate the filters used by the demodulators.
*
*----------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "direwolf.h"
#include "audio.h"
#include "fsk_demod_state.h"
#include "fsk_gen_filter.h"
#include "textcolor.h"
#include "dsp.h"
//#include "fsk_demod_agc.h" /* for M_FILTER_SIZE, etc. */
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
// Don't remove this. It serves as a reminder that an experiment is underway.
#if defined(TUNE_MS_FILTER_SIZE) || defined(TUNE_AGC_FAST) || defined(TUNE_LPF_BAUD) || defined(TUNE_PLL_LOCKED) || defined(TUNE_PROFILE)
#define DEBUG1 1
#endif
/*------------------------------------------------------------------
*
* Name: window
*
* Purpose: Filter window shape functions.
*
* Inputs: type - BP_WINDOW_HAMMING, etc.
* size - Number of filter taps.
* j - Index in range of 0 to size-1.
*
* Returns: Multiplier for the window shape.
*
*----------------------------------------------------------------*/
float window (bp_window_t type, int size, int j)
{
float center;
float w;
center = 0.5 * (size - 1);
switch (type) {
case BP_WINDOW_COSINE:
w = cos((j - center) / size * M_PI);
//w = sin(j * M_PI / (size - 1));
break;
case BP_WINDOW_HAMMING:
w = 0.53836 - 0.46164 * cos((j * 2 * M_PI) / (size - 1));
break;
case BP_WINDOW_BLACKMAN:
w = 0.42659 - 0.49656 * cos((j * 2 * M_PI) / (size - 1))
+ 0.076849 * cos((j * 4 * M_PI) / (size - 1));
break;
case BP_WINDOW_FLATTOP:
w = 1.0 - 1.93 * cos((j * 2 * M_PI) / (size - 1))
+ 1.29 * cos((j * 4 * M_PI) / (size - 1))
- 0.388 * cos((j * 6 * M_PI) / (size - 1))
+ 0.028 * cos((j * 8 * M_PI) / (size - 1));
break;
case BP_WINDOW_TRUNCATED:
default:
w = 1.0;
break;
}
return (w);
}
/*------------------------------------------------------------------
*
* Name: gen_lowpass
*
* Purpose: Generate low pass filter kernel.
*
* Inputs: fc - Cutoff frequency as fraction of sampling frequency.
* filter_size - Number of filter taps.
* wtype - Window type, BP_WINDOW_HAMMING, etc.
*
* Outputs: lp_filter
*
*----------------------------------------------------------------*/
void gen_lowpass (float fc, float *lp_filter, int filter_size, bp_window_t wtype)
{
int j;
float G;
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Lowpass, size=%d, fc=%.2f\n", filter_size, fc);
dw_printf (" j shape sinc final\n");
#endif
assert (filter_size >= 3 && filter_size <= MAX_FILTER_SIZE);
for (j=0; j<filter_size; j++) {
float center;
float sinc;
float shape;
center = 0.5 * (filter_size - 1);
if (j - center == 0) {
sinc = 2 * fc;
}
else {
sinc = sin(2 * M_PI * fc * (j-center)) / (M_PI*(j-center));
}
shape = window (wtype, filter_size, j);
lp_filter[j] = sinc * shape;
#if DEBUG1
dw_printf ("%6d %6.2f %6.3f %6.3f\n", j, shape, sinc, lp_filter[j] ) ;
#endif
}
/*
* Normalize lowpass for unity gain at DC.
*/
G = 0;
for (j=0; j<filter_size; j++) {
G += lp_filter[j];
}
for (j=0; j<filter_size; j++) {
lp_filter[j] = lp_filter[j] / G;
}
}
/*------------------------------------------------------------------
*
* Name: gen_bandpass
*
* Purpose: Generate band pass filter kernel.
*
* Inputs: f1 - Lower cutoff frequency as fraction of sampling frequency.
* f2 - Upper cutoff frequency...
* filter_size - Number of filter taps.
* wtype - Window type, BP_WINDOW_HAMMING, etc.
*
* Outputs: bp_filter
*
* Reference: http://www.labbookpages.co.uk/audio/firWindowing.html
*
* Does it need to be an odd length?
*
*----------------------------------------------------------------*/
void gen_bandpass (float f1, float f2, float *bp_filter, int filter_size, bp_window_t wtype)
{
int j;
float w;
float G;
float center = 0.5 * (filter_size - 1);
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Bandpass, size=%d\n", filter_size);
dw_printf (" j shape sinc final\n");
#endif
assert (filter_size >= 3 && filter_size <= MAX_FILTER_SIZE);
for (j=0; j<filter_size; j++) {
float sinc;
float shape;
if (j - center == 0) {
sinc = 2 * (f2 - f1);
}
else {
sinc = sin(2 * M_PI * f2 * (j-center)) / (M_PI*(j-center))
- sin(2 * M_PI * f1 * (j-center)) / (M_PI*(j-center));
}
shape = window (wtype, filter_size, j);
bp_filter[j] = sinc * shape;
#if DEBUG1
dw_printf ("%6d %6.2f %6.3f %6.3f\n", j, shape, sinc, bp_filter[j] ) ;
#endif
}
/*
* Normalize bandpass for unity gain in middle of passband.
* Can't use same technique as for lowpass.
* Instead compute gain in middle of passband.
* See http://dsp.stackexchange.com/questions/4693/fir-filter-gain
*/
w = 2 * M_PI * (f1 + f2) / 2;
G = 0;
for (j=0; j<filter_size; j++) {
G += 2 * bp_filter[j] * cos((j-center)*w); // is this correct?
}
#if DEBUG1
dw_printf ("Before normalizing, G=%.3f\n", G);
#endif
for (j=0; j<filter_size; j++) {
bp_filter[j] = bp_filter[j] / G;
}
}
/* end dsp.c */