kopia lustrzana https://github.com/kgoba/ft8_lib
Second step to moving to C
rodzic
2dbaa4672b
commit
dd43982dbd
|
@ -1,17 +1,17 @@
|
|||
#include "wave.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
// Save signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers.
|
||||
void save_wav(const float *signal, int num_samples, int sample_rate, const char *path) {
|
||||
void save_wav(const float *signal, int num_samples, int sample_rate, const char *path)
|
||||
{
|
||||
char subChunk1ID[4] = {'f', 'm', 't', ' '};
|
||||
uint32_t subChunk1Size = 16; // 16 for PCM
|
||||
uint16_t audioFormat = 1; // PCM = 1
|
||||
uint32_t subChunk1Size = 16; // 16 for PCM
|
||||
uint16_t audioFormat = 1; // PCM = 1
|
||||
uint16_t numChannels = 1;
|
||||
uint16_t bitsPerSample = 16;
|
||||
uint32_t sampleRate = sample_rate;
|
||||
|
@ -26,10 +26,13 @@ void save_wav(const float *signal, int num_samples, int sample_rate, const char
|
|||
char format[4] = {'W', 'A', 'V', 'E'};
|
||||
|
||||
int16_t *raw_data = (int16_t *)malloc(num_samples * blockAlign);
|
||||
for (int i = 0; i < num_samples; i++) {
|
||||
for (int i = 0; i < num_samples; i++)
|
||||
{
|
||||
float x = signal[i];
|
||||
if (x > 1.0) x = 1.0;
|
||||
else if (x < -1.0) x = -1.0;
|
||||
if (x > 1.0)
|
||||
x = 1.0;
|
||||
else if (x < -1.0)
|
||||
x = -1.0;
|
||||
raw_data[i] = int(0.5 + (x * 32767.0));
|
||||
}
|
||||
|
||||
|
@ -59,24 +62,24 @@ void save_wav(const float *signal, int num_samples, int sample_rate, const char
|
|||
free(raw_data);
|
||||
}
|
||||
|
||||
|
||||
// Load signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers.
|
||||
int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path) {
|
||||
int load_wav(float *signal, int *num_samples, int *sample_rate, const char *path)
|
||||
{
|
||||
char subChunk1ID[4]; // = {'f', 'm', 't', ' '};
|
||||
uint32_t subChunk1Size; // = 16; // 16 for PCM
|
||||
uint16_t audioFormat; // = 1; // PCM = 1
|
||||
uint16_t numChannels; // = 1;
|
||||
uint16_t bitsPerSample; // = 16;
|
||||
uint32_t sampleRate;
|
||||
uint16_t blockAlign; // = numChannels * bitsPerSample / 8;
|
||||
uint32_t byteRate; // = sampleRate * blockAlign;
|
||||
uint16_t blockAlign; // = numChannels * bitsPerSample / 8;
|
||||
uint32_t byteRate; // = sampleRate * blockAlign;
|
||||
|
||||
char subChunk2ID[4]; // = {'d', 'a', 't', 'a'};
|
||||
uint32_t subChunk2Size; // = num_samples * blockAlign;
|
||||
|
||||
char chunkID[4]; // = {'R', 'I', 'F', 'F'};
|
||||
uint32_t chunkSize; // = 4 + (8 + subChunk1Size) + (8 + subChunk2Size);
|
||||
char format[4]; // = {'W', 'A', 'V', 'E'};
|
||||
char chunkID[4]; // = {'R', 'I', 'F', 'F'};
|
||||
uint32_t chunkSize; // = 4 + (8 + subChunk1Size) + (8 + subChunk2Size);
|
||||
char format[4]; // = {'W', 'A', 'V', 'E'};
|
||||
|
||||
FILE *f = fopen(path, "rb");
|
||||
|
||||
|
@ -87,7 +90,8 @@ int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path
|
|||
|
||||
fread((void *)subChunk1ID, sizeof(subChunk1ID), 1, f);
|
||||
fread((void *)&subChunk1Size, sizeof(subChunk1Size), 1, f);
|
||||
if (subChunk1Size != 16) return -1;
|
||||
if (subChunk1Size != 16)
|
||||
return -1;
|
||||
|
||||
fread((void *)&audioFormat, sizeof(audioFormat), 1, f);
|
||||
fread((void *)&numChannels, sizeof(numChannels), 1, f);
|
||||
|
@ -96,23 +100,26 @@ int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path
|
|||
fread((void *)&blockAlign, sizeof(blockAlign), 1, f);
|
||||
fread((void *)&bitsPerSample, sizeof(bitsPerSample), 1, f);
|
||||
|
||||
if (audioFormat != 1 || numChannels != 1 || bitsPerSample != 16) return -1;
|
||||
if (audioFormat != 1 || numChannels != 1 || bitsPerSample != 16)
|
||||
return -1;
|
||||
|
||||
fread((void *)subChunk2ID, sizeof(subChunk2ID), 1, f);
|
||||
fread((void *)&subChunk2Size, sizeof(subChunk2Size), 1, f);
|
||||
|
||||
if (subChunk2Size / blockAlign > num_samples) return -2;
|
||||
|
||||
num_samples = subChunk2Size / blockAlign;
|
||||
sample_rate = sampleRate;
|
||||
if (subChunk2Size / blockAlign > *num_samples)
|
||||
return -2;
|
||||
|
||||
int16_t *raw_data = (int16_t *)malloc(num_samples * blockAlign);
|
||||
*num_samples = subChunk2Size / blockAlign;
|
||||
*sample_rate = sampleRate;
|
||||
|
||||
fread((void *)raw_data, blockAlign, num_samples, f);
|
||||
for (int i = 0; i < num_samples; i++) {
|
||||
int16_t *raw_data = (int16_t *)malloc(*num_samples * blockAlign);
|
||||
|
||||
fread((void *)raw_data, blockAlign, *num_samples, f);
|
||||
for (int i = 0; i < *num_samples; i++)
|
||||
{
|
||||
signal[i] = raw_data[i] / 32768.0f;
|
||||
}
|
||||
|
||||
|
||||
free(raw_data);
|
||||
|
||||
fclose(f);
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef _INCLUDE_WAVE_H_
|
||||
#define _INCLUDE_WAVE_H_
|
||||
|
||||
// Save signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers.
|
||||
void save_wav(const float *signal, int num_samples, int sample_rate, const char *path);
|
||||
|
||||
|
||||
// Load signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers.
|
||||
int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path);
|
||||
int load_wav(float *signal, int *num_samples, int *sample_rate, const char *path);
|
||||
|
||||
#endif // _INCLUDE_WAVE_H_
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "ft8/unpack.h"
|
||||
#include "ft8/ldpc.h"
|
||||
|
@ -192,7 +193,7 @@ int main(int argc, char **argv)
|
|||
int num_samples = 15 * sample_rate;
|
||||
float signal[num_samples];
|
||||
|
||||
int rc = load_wav(signal, num_samples, sample_rate, wav_path);
|
||||
int rc = load_wav(signal, &num_samples, &sample_rate, wav_path);
|
||||
if (rc < 0)
|
||||
{
|
||||
return -1;
|
||||
|
@ -229,12 +230,12 @@ int main(int argc, char **argv)
|
|||
int num_decoded = 0;
|
||||
for (int idx = 0; idx < num_candidates; ++idx)
|
||||
{
|
||||
Candidate &cand = candidate_list[idx];
|
||||
if (cand.score < kMin_score)
|
||||
const Candidate *cand = &candidate_list[idx];
|
||||
if (cand->score < kMin_score)
|
||||
continue;
|
||||
|
||||
float freq_hz = (cand.freq_offset + (float)cand.freq_sub / kFreq_osr) * kFSK_dev;
|
||||
float time_sec = (cand.time_offset + (float)cand.time_sub / kTime_osr) / kFSK_dev;
|
||||
float freq_hz = (cand->freq_offset + (float)cand->freq_sub / kFreq_osr) * kFSK_dev;
|
||||
float time_sec = (cand->time_offset + (float)cand->time_sub / kTime_osr) / kFSK_dev;
|
||||
|
||||
float log174[FT8_N];
|
||||
extract_likelihood(&power, cand, kGray_map, log174);
|
||||
|
@ -313,7 +314,7 @@ int main(int argc, char **argv)
|
|||
|
||||
// Fake WSJT-X-like output for now
|
||||
int snr = 0; // TODO: compute SNR
|
||||
printf("000000 %3d [%2d] %4.1f %4d ~ %s\n", cand.score, k, time_sec, (int)(freq_hz + 0.5f), message);
|
||||
printf("000000 %3d [%2d] %4.1f %4d ~ %s\n", cand->score, k, time_sec, (int)(freq_hz + 0.5f), message);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,11 +120,10 @@ int find_sync(const MagArray *power, const uint8_t *sync_map, int num_candidates
|
|||
|
||||
// Compute log likelihood log(p(1) / p(0)) of 174 message bits
|
||||
// for later use in soft-decision LDPC decoding
|
||||
void extract_likelihood(const MagArray *power, const Candidate &cand, const uint8_t *code_map, float *log174)
|
||||
void extract_likelihood(const MagArray *power, const Candidate *cand, const uint8_t *code_map, float *log174)
|
||||
{
|
||||
int num_alt = power->time_osr * power->freq_osr;
|
||||
// int offset = (cand.time_offset * num_alt + cand.time_sub * power->freq_osr + cand.freq_sub) * power->num_bins + cand.freq_offset;
|
||||
int offset = get_index(power, cand.time_offset, cand.time_sub, cand.freq_sub, cand.freq_offset);
|
||||
int offset = get_index(power, cand->time_offset, cand->time_sub, cand->freq_sub, cand->freq_offset);
|
||||
|
||||
// Go over FSK tones and skip Costas sync symbols
|
||||
const int n_syms = 1;
|
||||
|
|
|
@ -27,6 +27,6 @@ int find_sync(const MagArray *power, const uint8_t *sync_map, int num_candidates
|
|||
|
||||
// Compute log likelihood log(p(1) / p(0)) of 174 message bits
|
||||
// for later use in soft-decision LDPC decoding
|
||||
void extract_likelihood(const MagArray *power, const Candidate &cand, const uint8_t *code_map, float *log174);
|
||||
void extract_likelihood(const MagArray *power, const Candidate *cand, const uint8_t *code_map, float *log174);
|
||||
|
||||
#endif // _INCLUDE_DECODE_H_
|
||||
|
|
|
@ -29,7 +29,7 @@ void fmtmsg(char *msg_out, const char *msg_in);
|
|||
int dd_to_int(const char *str, int length);
|
||||
|
||||
// Convert a 2 digit integer to string
|
||||
void int_to_dd(char *str, int value, int width, bool full_sign = false);
|
||||
void int_to_dd(char *str, int value, int width, bool full_sign);
|
||||
|
||||
char charn(int c, int table_idx);
|
||||
int nchar(char c, int table_idx);
|
||||
|
|
|
@ -29,7 +29,7 @@ int unpack28(uint32_t n28, uint8_t ip, uint8_t i3, char *result)
|
|||
{
|
||||
// CQ_nnn with 3 digits
|
||||
strcpy(result, "CQ ");
|
||||
int_to_dd(result + 3, n28 - 3, 3);
|
||||
int_to_dd(result + 3, n28 - 3, 3, false);
|
||||
return 0; // Success
|
||||
}
|
||||
if (n28 <= 532443L)
|
||||
|
@ -63,7 +63,7 @@ int unpack28(uint32_t n28, uint8_t ip, uint8_t i3, char *result)
|
|||
// TODO: implement
|
||||
// strcpy(result, "<...>");
|
||||
result[0] = '<';
|
||||
int_to_dd(result + 1, n28, 7);
|
||||
int_to_dd(result + 1, n28, 7, false);
|
||||
result[8] = '>';
|
||||
result[9] = '\0';
|
||||
return 0;
|
||||
|
@ -303,7 +303,7 @@ int unpack_nonstandard(const uint8_t *a77, char *field1, char *field2, char *fie
|
|||
// should replace with hash12(n12, call_3);
|
||||
// strcpy(call_3, "<...>");
|
||||
call_3[0] = '<';
|
||||
int_to_dd(call_3 + 1, n12, 4);
|
||||
int_to_dd(call_3 + 1, n12, 4, false);
|
||||
call_3[5] = '>';
|
||||
call_3[6] = '\0';
|
||||
|
||||
|
|
11
gen_ft8.cpp
11
gen_ft8.cpp
|
@ -1,12 +1,11 @@
|
|||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "common/wave.h"
|
||||
#include "common/debug.h"
|
||||
//#include "ft8/v1/pack.h"
|
||||
//#include "ft8/v1/encode.h"
|
||||
#include "ft8/pack.h"
|
||||
#include "ft8/encode.h"
|
||||
#include "ft8/constants.h"
|
||||
|
|
12
test.cpp
12
test.cpp
|
@ -1,12 +1,10 @@
|
|||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "ft8/text.h"
|
||||
//#include "ft8/v1/pack.h"
|
||||
//#include "ft8/v1/unpack.h"
|
||||
//#include "ft8/v1/encode.h"
|
||||
#include "ft8/pack.h"
|
||||
#include "ft8/encode.h"
|
||||
#include "ft8/constants.h"
|
||||
|
|
Ładowanie…
Reference in New Issue