public static int GetBytePerSample(eSampleFormat format) { switch (format) { case eSampleFormat.Direct16BitIQFixedPointLE: case eSampleFormat.Direct16BitIQFixedPointBE: return(2); case eSampleFormat.Direct24BitIQFixedPointLE: case eSampleFormat.Direct24BitIQFixedPointBE: return(3); case eSampleFormat.Direct32BitIQFloat: case eSampleFormat.Direct32BitIQFloat64k: return(4); default: return(0); } }
public static void SamplesToBinary(byte[] dataBuffer, int samplePairs, double[] samplesI, double[] samplesQ, eSampleFormat dataFormat, bool invertedSpectrum) { if (UseNative) { try { SamplesToBinaryNative(dataBuffer, samplePairs, samplesI, samplesQ, (int)dataFormat, invertedSpectrum); } catch (Exception) { UseNative = false; SamplesToBinary(dataBuffer, samplePairs, samplesI, samplesQ, dataFormat, invertedSpectrum); return; } } else { int bytesPerSample; int bytesPerSamplePair; bytesPerSamplePair = GetBytePerSamplePair(dataFormat); bytesPerSample = GetBytePerSample(dataFormat); if (samplesI.Length < samplePairs || samplesQ.Length < samplePairs) { return; } for (int pos = 0; pos < samplePairs; pos++) { double I = samplesI[pos]; double Q = samplesQ[pos]; if (invertedSpectrum) { I = -I; } switch (dataFormat) { case eSampleFormat.Direct16BitIQFixedPointLE: putBytesFromDouble(dataBuffer, bytesPerSamplePair * pos, I); putBytesFromDouble(dataBuffer, bytesPerSamplePair * pos + bytesPerSample, Q); break; case eSampleFormat.Direct24BitIQFixedPointLE: break; case eSampleFormat.Direct32BitIQFloat: byte[] bufI = BitConverter.GetBytes((float)I); byte[] bufQ = BitConverter.GetBytes((float)Q); Array.Copy(bufI, 0, dataBuffer, bytesPerSamplePair * pos, bytesPerSample); Array.Copy(bufQ, 0, dataBuffer, bytesPerSamplePair * pos + bytesPerSample, bytesPerSample); break; case eSampleFormat.Direct32BitIQFloat64k: byte[] bufI64k = BitConverter.GetBytes((float)(I * 65536)); byte[] bufQ64k = BitConverter.GetBytes((float)(Q * 65536)); Array.Copy(bufI64k, 0, dataBuffer, bytesPerSamplePair * pos, bytesPerSample); Array.Copy(bufQ64k, 0, dataBuffer, bytesPerSamplePair * pos + bytesPerSample, bytesPerSample); break; default: return; } } return; } }
public static void SamplesToBinary(byte[] dataBuffer, double[] samplesI, double[] samplesQ, eSampleFormat dataFormat, bool InvertedSpectrum) { SamplesToBinary(dataBuffer, samplesI.Length, samplesI, samplesQ, dataFormat, InvertedSpectrum); }
public static void SamplesFromBinary(byte[] dataBuffer, int bytesRead, double[] samplesI, double[] samplesQ, eSampleFormat dataFormat, bool invertedSpectrum) { if (UseNative) { try { SamplesFromBinaryNative(dataBuffer, bytesRead, samplesI.Length, samplesI, samplesQ, (int)dataFormat, invertedSpectrum); } catch (Exception) { UseNative = false; SamplesFromBinary(dataBuffer, bytesRead, samplesI, samplesQ, dataFormat, invertedSpectrum); return; } } else { int bytesPerSample; int bytesPerSamplePair; bytesPerSamplePair = GetBytePerSamplePair(dataFormat); bytesPerSample = GetBytePerSample(dataFormat); int samplePos = 0; int samplePairs = bytesRead / bytesPerSamplePair; if (samplesI.Length < samplePairs || samplesQ.Length < samplePairs) { return; } for (int pos = 0; pos < samplePairs; pos++) { double I; double Q; switch (dataFormat) { case eSampleFormat.Direct16BitIQFixedPointLE: I = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos, 2, true); Q = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos + bytesPerSample, 2, true); break; case eSampleFormat.Direct16BitIQFixedPointBE: I = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos, 2, false); Q = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos + bytesPerSample, 2, false); break; case eSampleFormat.Direct24BitIQFixedPointLE: I = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos, 3, true); Q = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos + bytesPerSample, 3, true); break; case eSampleFormat.Direct24BitIQFixedPointBE: I = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos, 3, false); Q = getDoubleFromBytes(dataBuffer, bytesPerSamplePair * pos + bytesPerSample, 3, false); break; case eSampleFormat.Direct32BitIQFloat: I = BitConverter.ToSingle(dataBuffer, bytesPerSamplePair * pos); Q = BitConverter.ToSingle(dataBuffer, bytesPerSamplePair * pos + bytesPerSample); break; case eSampleFormat.Direct32BitIQFloat64k: I = BitConverter.ToSingle(dataBuffer, bytesPerSamplePair * pos) / 65536; Q = BitConverter.ToSingle(dataBuffer, bytesPerSamplePair * pos + bytesPerSample) / 65536; break; default: return; } if (invertedSpectrum) { I = -I; } samplesI[pos] = I; samplesQ[pos] = Q; } return; } }