Store data in ( .wav) format from RTL-SDR device using c#

1k Views Asked by At

I am able to connect with RTL-SDR using librtlsdr.dll and libusb-1.0.dll by using (https://github.com/nandortoth/rtlsdr-manager) wrapper in c# .netcore3.0 .

Started getting sampledata from device by set device frequency. i am getting data in List of IQ .

I need to store this data in .wav file .its very easy with chrome.usb.bulktransfer function. this function provide int8array,int32array,uint8array that is directly write able to .wav file .

i don't have an idea how it could be done using c# from IQ array

any suggestions or code samples will be appreciated.

1

There are 1 best solutions below

0
On

To save IQ data to .wav file, you can write I component to one channel and Q component to the other channel (reference).

The "in-phase" (real) is typically placed in the left channel and the "quadrature" (imag) part in the right channel.

You can use NAudio to achieve that. First, you need BinaryWriter to convert int array (IQ data) to Stream (byte array). Then, you can use MultiplexingWaveProvider to create .wav file with 2 channels.

var iqDataList = device.GetSamplesFromAsyncBuffer(maxCount);

var iStream = new MemoryStream();
var qStream = new MemoryStream();

var iWriter = new BinaryWriter(iStream);
var qWriter = new BinaryWriter(qStream);

foreach (var iqData in iqDataList)
{
    iWriter.Write(iqData.I);
    qWriter.Write(iqData.Q);
}

var iComponent = new RawSourceWaveStream(iStream, new WaveFormat(rate, bits, 1));
var qComponent = new RawSourceWaveStream(qStream, new WaveFormat(rate, bits, 1));

var waveProvider = new MultiplexingWaveProvider(new IWaveProvider[] {iComponent, qComponent}, 2);

WaveFileWriter.CreateWaveFile(filename, waveProvider);

iComponent.Dispose();
qComponent.Dispose();

iWriter.Dispose();
qWriter.Dispose();