miweber67 / spyserver_client

CLI spyserver client to provide basic rtl_sdr and rtl_power-like outputs from an Airspy spyserver.
GNU General Public License v3.0
25 stars 2 forks source link

Better amplitude resolution in the spectrum power #10

Open helioshk opened 1 year ago

helioshk commented 1 year ago

It would be nice to have a better resolution than 1 dB (int) steps.

I asked chatgpt do do the modification. Not sure if it is proper, but it seems to be working and looks good. So I'll just leave it here. diff:

369,425c369,413
<       if( now - last_start > settings.fft_average_seconds ) {
<          double hz_step = bandwidth / fft_data.size();
<          double fft_hz_low = settings.center_freq - (bandwidth / 2.0);
<          double fft_hz_high = settings.center_freq + (bandwidth / 2.0);
<          double hz_low = fft_hz_low;
<          double hz_high = fft_hz_high;
< 
<          if( hz_low < settings.low_freq ) {
<             unsigned int lowsteps = std::ceil((settings.low_freq - fft_hz_low) / hz_step);
<             hz_low = fft_hz_low + (hz_step * lowsteps);
<             //std::cerr << "lowsteps: " << lowsteps << "\n";
<          }
<          if( hz_high > settings.high_freq ) {
<             unsigned int highsteps = std::ceil((settings.high_freq - fft_hz_low) / hz_step);
<             hz_high = fft_hz_low + (hz_step * highsteps);
<             //std::cerr << "highsteps: " << highsteps << "\n";
<          }
< 
<          // dump to output file
<          // create rtl_power-like header
<          // # date, time, Hz low, Hz high, Hz step, samples, dB, dB, dB, ...
<          // need only hz low and hz step
<          std::ofstream outfile (settings.fft_outfilename);
<          outfile << "date, time, " << (unsigned int)hz_low << ", "
<                  << (unsigned int)hz_high << ", "
<                  << hz_step << ", "
<                  << "1";
< 
<          size_t num_pts = fft_data_sums.size();
< //         std::cerr << "processing " << num_pts << " points from " << fft_hz_low
< //                   << " to " << fft_hz_high << std::endl;
<          
< //         uint32_t wrote = 0;
<          for (size_t i = 0; i < num_pts; ++i)
<          {
<             double cur_hz = fft_hz_low + (hz_step * i);
<             if( cur_hz >= hz_low && cur_hz <= hz_high ) {
<                outfile << ", " << (fft_data_sums[i] / sum_periods);
< //               ++wrote;
<             } else {
<                // nop
<             }
<             fft_data_sums[i] = 0;
<          }
<          outfile << std::endl;
< 
<          sum_periods = 0;
<          last_start = now;
<          
<          if( settings.oneshot == 1 ) {
<             outfile.close();
<             running = false;         
<          }
< 
< //         std::cerr << "log file updated; wrote " << wrote << " points out of " << fft_data.size() << std::endl;
<       
<       }
---
> if (now - last_start > settings.fft_average_seconds) {
>     double hz_step = bandwidth / fft_data.size();
>     double fft_hz_low = settings.center_freq - (bandwidth / 2.0);
>     double fft_hz_high = settings.center_freq + (bandwidth / 2.0);
>     double hz_low = fft_hz_low;
>     double hz_high = fft_hz_high;
> 
>     if (hz_low < settings.low_freq) {
>         unsigned int lowsteps = std::ceil((settings.low_freq - fft_hz_low) / hz_step);
>         hz_low = fft_hz_low + (hz_step * lowsteps);
>     }
>     if (hz_high > settings.high_freq) {
>         unsigned int highsteps = std::ceil((settings.high_freq - fft_hz_low) / hz_step);
>         hz_high = fft_hz_low + (hz_step * highsteps);
>     }
> 
>     // Dump to output file
>     // Create rtl_power-like header
>     // # date, time, Hz low, Hz high, Hz step, samples, dB, dB, dB, ...
>     // Need only hz low and hz step
>     std::ofstream outfile(settings.fft_outfilename);
>     outfile << "date, time, " << (unsigned int)hz_low << ", "
>             << (unsigned int)hz_high << ", "
>             << hz_step << ", "
>             << "1";
> 
>     size_t num_pts = fft_data_sums.size();
>     for (size_t i = 0; i < num_pts; ++i) {
>         double cur_hz = fft_hz_low + (hz_step * i);
>         if (cur_hz >= hz_low && cur_hz <= hz_high) {
>             outfile << ", " << static_cast<double>(fft_data_sums[i]) / sum_periods;
>         }
>         fft_data_sums[i] = 0;
>     }
>     outfile << std::endl;
> 
>     sum_periods = 0;
>     last_start = now;
> 
>     if (settings.oneshot == 1) {
>         outfile.close();
>         running = false;
>     }
> }
> 
miweber67 commented 9 months ago

If we do this, our output file values are no longer the same as rtl_power's format (float vs. int) and consumers of the csv file may break. Perhaps this could be made an option if there's interest.