kopia lustrzana https://github.com/rpp0/gr-lora
Improved sync, automatic CFO correction
rodzic
be8cba4ddb
commit
4a6b45c809
|
@ -174,6 +174,17 @@ namespace gr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double decoder_impl::cross_correlate(const gr_complex *samples_1, const gr_complex *samples_2, int window) {
|
||||||
|
double result = 0.0f;
|
||||||
|
|
||||||
|
for (int i = 0; i < window; i++) {
|
||||||
|
result += real(samples_1[i] * conj(samples_2[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
result = result / window;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
double decoder_impl::freq_cross_correlate(const gr_complex *samples_1, const gr_complex *samples_2, int window) {
|
double decoder_impl::freq_cross_correlate(const gr_complex *samples_1, const gr_complex *samples_2, int window) {
|
||||||
double result = 0.0f;
|
double result = 0.0f;
|
||||||
float instantaneous_phase[window];
|
float instantaneous_phase[window];
|
||||||
|
@ -207,27 +218,27 @@ namespace gr {
|
||||||
float fft_mag[d_number_of_bins];
|
float fft_mag[d_number_of_bins];
|
||||||
gr_complex mult_hf[d_samples_per_symbol];
|
gr_complex mult_hf[d_samples_per_symbol];
|
||||||
|
|
||||||
#ifdef CFO_CORRECT
|
/*#ifdef CFO_CORRECT
|
||||||
determine_cfo(&samples[0]);
|
determine_cfo(&samples[0]);
|
||||||
std::cout << "CFO: " << d_cfo_estimation << std::endl;
|
std::cout << "CFO: " << d_cfo_estimation << std::endl;
|
||||||
correct_cfo(&samples[0], d_samples_per_symbol);
|
correct_cfo(&samples[0], d_samples_per_symbol);
|
||||||
#endif
|
#endif*/
|
||||||
|
|
||||||
//samples_to_file("/tmp/data", &samples[0], d_samples_per_symbol, sizeof(gr_complex));
|
samples_to_file("/tmp/data", &samples[0], d_samples_per_symbol, sizeof(gr_complex));
|
||||||
|
|
||||||
// Multiply with ideal downchirp
|
// Multiply with ideal downchirp
|
||||||
for(uint32_t i = 0; i < d_samples_per_symbol; i++) {
|
for(uint32_t i = 0; i < d_samples_per_symbol; i++) {
|
||||||
mult_hf[i] = conj(samples[i] * d_downchirp[i]);
|
mult_hf[i] = conj(samples[i] * d_downchirp[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//samples_to_file("/tmp/mult", &mult_hf[0], d_samples_per_symbol, sizeof(gr_complex));
|
samples_to_file("/tmp/mult", &mult_hf[0], d_samples_per_symbol, sizeof(gr_complex));
|
||||||
|
|
||||||
// Perform decimation
|
// Perform decimation
|
||||||
for (uint32_t i = 0; i < d_number_of_bins; i++) {
|
for (uint32_t i = 0; i < d_number_of_bins; i++) {
|
||||||
firdecim_crcf_execute(d_decim, &mult_hf[d_decim_factor*i], &d_mult[i]);
|
firdecim_crcf_execute(d_decim, &mult_hf[d_decim_factor*i], &d_mult[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//samples_to_file("/tmp/resampled", &d_mult[0], d_number_of_bins, sizeof(gr_complex));
|
samples_to_file("/tmp/resampled", &d_mult[0], d_number_of_bins, sizeof(gr_complex));
|
||||||
|
|
||||||
// Perform FFT
|
// Perform FFT
|
||||||
fft_execute(d_q);
|
fft_execute(d_q);
|
||||||
|
@ -237,7 +248,7 @@ namespace gr {
|
||||||
fft_mag[i] = abs(d_fft[i]);
|
fft_mag[i] = abs(d_fft[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//samples_to_file("/tmp/fft", &d_fft[0], d_number_of_bins, sizeof(gr_complex));
|
samples_to_file("/tmp/fft", &d_fft[0], d_number_of_bins, sizeof(gr_complex));
|
||||||
|
|
||||||
// Return argmax here
|
// Return argmax here
|
||||||
return (std::max_element(fft_mag,fft_mag+d_number_of_bins) - fft_mag);
|
return (std::max_element(fft_mag,fft_mag+d_number_of_bins) - fft_mag);
|
||||||
|
@ -286,8 +297,8 @@ namespace gr {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool decoder_impl::demodulate(gr_complex* samples, bool is_header) {
|
bool decoder_impl::demodulate(gr_complex* samples, bool is_header) {
|
||||||
//unsigned int bin_idx = max_frequency_gradient_idx(samples);
|
unsigned int bin_idx = max_frequency_gradient_idx(samples);
|
||||||
unsigned int bin_idx = sync_fft(samples);
|
//unsigned int bin_idx = sync_fft(samples);
|
||||||
//unsigned int bin_idx_test = sync_fft(samples);
|
//unsigned int bin_idx_test = sync_fft(samples);
|
||||||
unsigned int bin_idx_test = 0;
|
unsigned int bin_idx_test = 0;
|
||||||
|
|
||||||
|
@ -467,6 +478,8 @@ namespace gr {
|
||||||
sum /= d_samples_per_symbol;
|
sum /= d_samples_per_symbol;
|
||||||
|
|
||||||
d_cfo_estimation = sum;
|
d_cfo_estimation = sum;
|
||||||
|
|
||||||
|
/*d_cfo_estimation = (*std::max_element(instantaneous_freq, instantaneous_freq+d_samples_per_symbol-1) + *std::min_element(instantaneous_freq, instantaneous_freq+d_samples_per_symbol-1)) / 2;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void decoder_impl::correct_cfo(gr_complex* samples, int num_samples) {
|
void decoder_impl::correct_cfo(gr_complex* samples, int num_samples) {
|
||||||
|
@ -485,9 +498,9 @@ namespace gr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int decoder_impl::find_preamble_start_fast(gr_complex* samples) {
|
int decoder_impl::find_preamble_start_fast(gr_complex* samples, uint32_t len) {
|
||||||
int step_size = d_samples_per_symbol / 8;
|
int step_size = d_samples_per_symbol / 8;
|
||||||
for(int i = 0; i < d_samples_per_symbol; i++) {
|
for(int i = 0; i < len; i += 8) {
|
||||||
bool higher = true;
|
bool higher = true;
|
||||||
float last_ifreq = -999999999;
|
float last_ifreq = -999999999;
|
||||||
|
|
||||||
|
@ -501,7 +514,7 @@ namespace gr {
|
||||||
float ifreq = (s[1] - s[0]) / (2.0f * M_PI) * d_samples_per_second;
|
float ifreq = (s[1] - s[0]) / (2.0f * M_PI) * d_samples_per_second;
|
||||||
d_debug << "F: " << ifreq << std::endl;
|
d_debug << "F: " << ifreq << std::endl;
|
||||||
|
|
||||||
if(ifreq - last_ifreq < (d_bw / 8) - 3000) { // Make sure it rises fast enough
|
if(ifreq - last_ifreq < (d_bw / 8) / 1.5) { // Make sure it rises fast enough
|
||||||
higher = false;
|
higher = false;
|
||||||
d_debug << "NOPE" << std::endl;
|
d_debug << "NOPE" << std::endl;
|
||||||
break;
|
break;
|
||||||
|
@ -528,23 +541,24 @@ namespace gr {
|
||||||
switch(d_state) {
|
switch(d_state) {
|
||||||
case DETECT: {
|
case DETECT: {
|
||||||
if(calc_energy_threshold(input, noutput_items, 0.002)) {
|
if(calc_energy_threshold(input, noutput_items, 0.002)) {
|
||||||
//d_debug << "Got something\n";
|
|
||||||
|
|
||||||
// Attempt to synchronize to an upchirp of the preamble
|
// Attempt to synchronize to an upchirp of the preamble
|
||||||
int chirp_start_pos = -1;
|
int chirp_start_pos = -1;
|
||||||
|
d_cfo_estimation = 0;
|
||||||
|
|
||||||
// Find rough position of preamble
|
// Find rough position of preamble
|
||||||
int i = find_preamble_start_fast(&input[0]);
|
int i = find_preamble_start_fast(&input[0], noutput_items);
|
||||||
|
|
||||||
// After this step, if i != -1 we know that we are in a rising chirp, starting from i.
|
// After this step, if i != -1 we know that we are in a rising chirp, starting from i.
|
||||||
// Calculate the CFO here, and correct for it. Then perform sync_fft until we get a 0
|
// Calculate the CFO here, and correct for it. Then perform sync_fft until we get a 0
|
||||||
// The final position where this is the case indicates the start of the preamble.
|
// The final position where this is the case indicates the start of the preamble.
|
||||||
if(i != -1) {
|
if(i != -1) {
|
||||||
//samples_to_file("/tmp/bcfo", &input[i], noutput_items, sizeof(gr_complex));
|
// TODO: Find algorithm to reliably determine CFO
|
||||||
|
samples_to_file("/tmp/bcfo", &input[0], noutput_items, sizeof(gr_complex));
|
||||||
|
i = find_preamble_start(&input[0]);
|
||||||
determine_cfo(&input[i]);
|
determine_cfo(&input[i]);
|
||||||
//d_debug << "CFO " << d_cfo_estimation << std::endl;
|
d_debug << "CFO " << d_cfo_estimation << std::endl;
|
||||||
correct_cfo(&input[0], noutput_items);
|
correct_cfo(&input[0], noutput_items);
|
||||||
//samples_to_file("/tmp/acfo", &input[i], noutput_items, sizeof(gr_complex));
|
samples_to_file("/tmp/acfo", &input[0], noutput_items, sizeof(gr_complex));
|
||||||
|
|
||||||
// Sync
|
// Sync
|
||||||
i = find_preamble_start(&input[0]);
|
i = find_preamble_start(&input[0]);
|
||||||
|
@ -565,8 +579,8 @@ namespace gr {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SYNC: {
|
case SYNC: {
|
||||||
double c = freq_cross_correlate(&input[0], &d_downchirp[0], CORRELATION_SEARCH_RANGE);
|
double c = freq_cross_correlate(&input[0], &d_downchirp[0], d_samples_per_symbol);
|
||||||
//d_debug << "C: " << c << std::endl;
|
d_debug << "C: " << c << std::endl;
|
||||||
|
|
||||||
if(c > 0.045f) {
|
if(c > 0.045f) {
|
||||||
d_debug << "SYNC: " << c << std::endl;
|
d_debug << "SYNC: " << c << std::endl;
|
||||||
|
@ -579,7 +593,6 @@ namespace gr {
|
||||||
d_corr_fails++;
|
d_corr_fails++;
|
||||||
if(d_corr_fails > 32) {
|
if(d_corr_fails > 32) {
|
||||||
d_state = DETECT;
|
d_state = DETECT;
|
||||||
d_cfo_estimation = 0;
|
|
||||||
}
|
}
|
||||||
consume_each(d_samples_per_symbol);
|
consume_each(d_samples_per_symbol);
|
||||||
}
|
}
|
||||||
|
@ -626,7 +639,6 @@ namespace gr {
|
||||||
decode(decoded, false);
|
decode(decoded, false);
|
||||||
|
|
||||||
d_state = DETECT;
|
d_state = DETECT;
|
||||||
d_cfo_estimation = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -84,11 +84,12 @@ namespace gr {
|
||||||
void samples_to_file(const std::string path, const gr_complex* v, int length, int elem_size);
|
void samples_to_file(const std::string path, const gr_complex* v, int length, int elem_size);
|
||||||
void samples_debug(const gr_complex* v, int length);
|
void samples_debug(const gr_complex* v, int length);
|
||||||
double freq_cross_correlate(const gr_complex *samples_1, const gr_complex *samples_2, int window);
|
double freq_cross_correlate(const gr_complex *samples_1, const gr_complex *samples_2, int window);
|
||||||
|
double cross_correlate(const gr_complex *samples_1, const gr_complex *samples_2, int window);
|
||||||
unsigned int sync_fft(gr_complex* samples);
|
unsigned int sync_fft(gr_complex* samples);
|
||||||
void determine_cfo(const gr_complex* samples);
|
void determine_cfo(const gr_complex* samples);
|
||||||
void correct_cfo(gr_complex* samples, int num_samples);
|
void correct_cfo(gr_complex* samples, int num_samples);
|
||||||
int find_preamble_start(gr_complex* samples);
|
int find_preamble_start(gr_complex* samples);
|
||||||
int find_preamble_start_fast(gr_complex* samples);
|
int find_preamble_start_fast(gr_complex* samples, uint32_t len);
|
||||||
unsigned int max_frequency_gradient_idx(gr_complex* samples);
|
unsigned int max_frequency_gradient_idx(gr_complex* samples);
|
||||||
bool demodulate(gr_complex* samples, bool is_header);
|
bool demodulate(gr_complex* samples, bool is_header);
|
||||||
void deinterleave(int ppm);
|
void deinterleave(int ppm);
|
||||||
|
|
Ładowanie…
Reference in New Issue