public override void ProcessWideBandData(ref byte[] EP4buf) { if (MainForm.doWideBand) // display wide band spectrum { // Read new buffer of full-bandwidth int ret = libUSB_Interface.usb_bulk_read(hdev, 0x84, EP4buf, Form1.EP4BufSize, 100); if (ret == Form1.EP4BufSize) { float scaleIn = (float)(1.0 / Math.Pow(2, 15)); float Sample, RealAverage; RealAverage = 0.0f; int i, k; for (i = 0, k = 0; i < Form1.EP4BufSize; i += 2, ++k) { // use this rather than BitConverter.ToInt16()...since its faster and less CPU intensive // without the '(short)' in there, the value doesn't get sign-extended! // so what should be small negative values show up as (almost) positive values? Sample = scaleIn * (float)(short)((EP4buf[i] << 8) | (EP4buf[i + 1])); RealAverage += Sample; SamplesReal[k] = Sample; } RealAverage /= (float)k; for (i = 0; i < k; ++i) { SamplesReal[i] -= RealAverage; // Subtract average SamplesImag[i] = SamplesReal[i]; // temporary -- soon will do digital down-conversion // with sin & cos, so we'll actually have I & Q data to enter. } FullBandwidthSpectrum.Process(SamplesReal, SamplesImag, Form1.EP4BufSize / 2); } } }
public override void ProcessWideBandData(ref byte[] EP4buf) { if (bWriteFullBandData && (fullBandData != null)) { // Open the stream for writing. try { // Add some information to the file. // write a timestamp (8 bytes) // the reverse action is done by: // DateTime d = DateTime.FromBinary(BitConverter.ToInt64(b, 0)) // where 'b' is an array of bytes of length 8. DateTime timestamp = DateTime.UtcNow; byte[] time = BitConverter.GetBytes(timestamp.Ticks); fullBandData.Write(time, 0, time.Length); // write number of bytes that follow (using 4 bytes for the length) // the reverse of this is: int length = Integer.FromBinary(DateTime d = DateTime.FromBinary(BitConverter.ToInt32(b, 0)) // where 'b' is an array of bytes of length 4. byte[] length = BitConverter.GetBytes(EP4buf.Length); fullBandData.Write(length, 0, length.Length); // write the data fullBandData.Write(EP4buf, 0, EP4buf.Length); // how to decode the file: // 1: read 8 bytes, and convert into a timestamp in UTC time // 2: read 4 bytes, and convert into an integer, call this 'length'. This is the number of bytes to read next. // 3: read 'length' bytes into a byte array. // 4: process the resulting data, exactly as it is processed below in the 'if (MainForm.doWideBand)' clause. } catch (Exception ex) { // if it's an IO exception, then the folder where the file is stored MAY be used // by other processes like an AntiVirus program scanning the file after it was closed, // or a program like 'WD Sync', which shadows your files to to a network or USB drive, // and may NOT use a shadow-copy mode that allows the file to be opened! // Best to configure these tools to store the file somewhere else that IS NOT // being managed by such programs MessageBox.Show("Likely there is a process like AntiVirus or 'WD Sync' that is accessing the file, preventing further writes. Reconfigure your programs and/or don't save the data file where it will be scanned or copied", "Another program is interfering"); MainForm.disableDataLogging(); } } if (MainForm.doWideBand) // display wide band spectrum { // EP4 contains 4k x 16 bit raw ADC samples // Actually it may contain 4 times that! (16k x 16 bit raw adc samples) { float scaleIn = (float)(1.0 / Math.Pow(2, 15)); float Sample, RealAverage; RealAverage = 0.0f; int numRawSamples = Form1.EP4BufSize; int numSamples = numRawSamples / 2; for (int i = 0, k = 0; i < numRawSamples; i += 2, ++k) { // use this rather than BitConverter.ToInt16()...since its faster and less CPU intensive // without the '(short)' in there, the value doesn't get sign-extended! // so what should be small negative values show up as (almost) positive values? Sample = scaleIn * (float)(short)((EP4buf[i + 1] << 8) | (EP4buf[i])); RealAverage += Sample; SamplesReal[k] = Sample; } RealAverage /= (float)numSamples; // normalize each real data value, based on the average of all samples, // and copy the Real value to the Imaginary array. for (int i = 0; i < numSamples; ++i) { SamplesReal[i] -= RealAverage; // Subtract average // temporary fake Imaginary data -- soon will do digital down-conversion // with sin & cos, so we'll actually have I & Q data to enter. SamplesImag[i] = SamplesReal[i]; } FullBandwidthSpectrum.Process(SamplesReal, SamplesImag, numSamples); } } }