int main(int argc, char *argv[]) { baseband_init(); uint8_t *cu8_buf; uint16_t *y16_buf; int16_t *cs16_buf; uint32_t *y32_buf; uint16_t *u16_buf; uint32_t *u32_buf; int16_t *s16_buf; int32_t *s32_buf; char *filename; long n_read; unsigned long n_samples; int max_block_size = 4096000; FilterState state; DemodFM_State fm_state; if (argc <= 1) { return 1; } filename = argv[1]; cu8_buf = malloc(sizeof(uint8_t) * 2 * max_block_size); y16_buf = malloc(sizeof(uint16_t) * max_block_size); cs16_buf = malloc(sizeof(int16_t) * 2 * max_block_size); y32_buf = malloc(sizeof(uint32_t) * max_block_size); u16_buf = malloc(sizeof(uint16_t) * max_block_size); u32_buf = malloc(sizeof(uint32_t) * max_block_size); s16_buf = malloc(sizeof(int16_t) * max_block_size); s32_buf = malloc(sizeof(int32_t) * max_block_size); n_read = read_buf(filename, cu8_buf, sizeof(uint8_t) * 2 * max_block_size); if (n_read < 1) { return 1; } n_samples = n_read / (sizeof(uint8_t) * 2); for (unsigned long i = 0; i < n_samples * 2; i++) { //cs16_buf[i] = 127 - cu8_buf[i]; //cs16_buf[i] = (int16_t)cu8_buf[i] * 16 - 2040; cs16_buf[i] = (int16_t)cu8_buf[i] * 128 - 16320; //cs16_buf[i] = (int16_t)cu8_buf[i] * 256 - 32640; } MEASURE("envelope_detect", envelope_detect(cu8_buf, y16_buf, n_samples); );
static void rtlsdr_callback(unsigned char *iq_buf, uint32_t len, void *ctx) { struct dm_state *demod = ctx; int i; if (do_exit || do_exit_async) return; if ((bytes_to_read > 0) && (bytes_to_read < len)) { len = bytes_to_read; do_exit = 1; rtlsdr_cancel_async(dev); } if (demod->signal_grabber) { //fprintf(stderr, "[%d] sg_index - len %d\n", demod->sg_index, len ); memcpy(&demod->sg_buf[demod->sg_index], iq_buf, len); demod->sg_len = len; demod->sg_index += len; if (demod->sg_index + len > SIGNAL_GRABBER_BUFFER) demod->sg_index = 0; } // AM demodulation envelope_detect(iq_buf, demod->temp_buf, len/2); baseband_low_pass_filter(demod->temp_buf, demod->am_buf, len/2, &demod->lowpass_filter_state); // FM demodulation if (demod->enable_FM_demod) { baseband_demod_FM(iq_buf, demod->fm_buf, len/2, &demod->demod_FM_state); } // Handle special input formats if(!demod->out_file) { // If output file is specified we always assume I/Q input if (demod->debug_mode == 1) { // The IQ buffer is really AM demodulated data memcpy(demod->am_buf, iq_buf, len); } else if (demod->debug_mode == 2) { // The IQ buffer is really FM demodulated data fprintf(stderr, "Reading FM modulated data not implemented yet!\n"); } } if (demod->analyze || (demod->out_file == stdout)) { // We don't want to decode devices when outputting to stdout pwm_analyze(demod, demod->am_buf, len / 2); } else { // Detect a package and loop through demodulators with pulse data int package_type = 1; // Just to get us started while(package_type) { package_type = pulse_detect_package(demod->am_buf, demod->fm_buf, len/2, demod->level_limit, samp_rate, &demod->pulse_data, &demod->fsk_pulse_data); if (package_type == 1) { if(demod->analyze_pulses) fprintf(stderr, "Detected OOK package\n"); for (i = 0; i < demod->r_dev_num; i++) { switch (demod->r_devs[i]->modulation) { case OOK_PULSE_PCM_RZ: pulse_demod_pcm(&demod->pulse_data, demod->r_devs[i]); break; case OOK_PULSE_PPM_RAW: pulse_demod_ppm(&demod->pulse_data, demod->r_devs[i]); break; case OOK_PULSE_PWM_PRECISE: pulse_demod_pwm_precise(&demod->pulse_data, demod->r_devs[i]); break; case OOK_PULSE_PWM_RAW: pulse_demod_pwm(&demod->pulse_data, demod->r_devs[i]); break; case OOK_PULSE_PWM_TERNARY: pulse_demod_pwm_ternary(&demod->pulse_data, demod->r_devs[i]); break; case OOK_PULSE_MANCHESTER_ZEROBIT: pulse_demod_manchester_zerobit(&demod->pulse_data, demod->r_devs[i]); break; case OOK_PULSE_CLOCK_BITS: pulse_demod_clock_bits(&demod->pulse_data, demod->r_devs[i]); break; case OOK_PULSE_PWM_OSV1: pulse_demod_osv1(&demod->pulse_data, demod->r_devs[i]); break; // FSK decoders case FSK_PULSE_PCM: case FSK_PULSE_PWM_RAW: break; default: fprintf(stderr, "Unknown modulation %d in protocol!\n", demod->r_devs[i]->modulation); } } // for demodulators if(debug_output > 1) pulse_data_print(&demod->pulse_data); if(demod->analyze_pulses) pulse_analyzer(&demod->pulse_data, samp_rate); } else if (package_type == 2) { if(demod->analyze_pulses) fprintf(stderr, "Detected FSK package\n"); for (i = 0; i < demod->r_dev_num; i++) { switch (demod->r_devs[i]->modulation) { // OOK decoders case OOK_PULSE_PCM_RZ: case OOK_PULSE_PPM_RAW: case OOK_PULSE_PWM_PRECISE: case OOK_PULSE_PWM_RAW: case OOK_PULSE_PWM_TERNARY: case OOK_PULSE_MANCHESTER_ZEROBIT: case OOK_PULSE_CLOCK_BITS: case OOK_PULSE_PWM_OSV1: break; case FSK_PULSE_PCM: pulse_demod_pcm(&demod->fsk_pulse_data, demod->r_devs[i]); break; case FSK_PULSE_PWM_RAW: pulse_demod_pwm(&demod->fsk_pulse_data, demod->r_devs[i]); break; default: fprintf(stderr, "Unknown modulation %d in protocol!\n", demod->r_devs[i]->modulation); } } // for demodulators if(debug_output > 1) pulse_data_print(&demod->fsk_pulse_data); if(demod->analyze_pulses) pulse_analyzer(&demod->fsk_pulse_data, samp_rate); } } } if (demod->out_file) { uint8_t* out_buf = iq_buf; // Default is to dump IQ samples if (demod->debug_mode == 1) { // AM data out_buf = (uint8_t*)demod->am_buf; } else if (demod->debug_mode == 2) { // FM data out_buf = (uint8_t*)demod->fm_buf; } if (fwrite(out_buf, 1, len, demod->out_file) != len) { fprintf(stderr, "Short write, samples lost, exiting!\n"); rtlsdr_cancel_async(dev); } } if (bytes_to_read > 0) bytes_to_read -= len; time_t rawtime; time(&rawtime); if (frequencies > 1) { if (difftime(rawtime, rawtime_old) > DEFAULT_HOP_TIME || events >= DEFAULT_HOP_EVENTS) { rawtime_old = rawtime; events = 0; do_exit_async = 1; rtlsdr_cancel_async(dev); } } if (duration > 0 && rawtime >= stop_time) { do_exit_async = do_exit = 1; fprintf(stderr, "Time expired, exiting!\n"); rtlsdr_cancel_async(dev); } }