public static Waveform LoadWaveformFromTDMS(string filePath, string waveformName = "", bool normalizeWaveform = true)
        {
            Waveform waveform = new Waveform();

            if (string.IsNullOrEmpty(waveformName))
            {
                waveformName = Path.GetFileNameWithoutExtension(filePath);
                Utilities.FormatWaveformName(ref waveformName);
            }
            waveform.WaveformName = waveformName;
            NIRfsgPlayback.ReadWaveformFromFileComplex(filePath, ref waveform.WaveformData);
            NIRfsgPlayback.ReadSignalBandwidthFromFile(filePath, 0, out waveform.SignalBandwidth_Hz);

            NIRfsgPlayback.ReadWaveformFileVersionFromFile(filePath, out string waveformVersion);
            if (waveformVersion == "1.0.0")
            {
                NIRfsgPlayback.ReadPeakPowerAdjustmentFromFile(filePath, 0, out waveform.PAPR_dB);
            }
            else
            {
                NIRfsgPlayback.ReadPaprFromFile(filePath, 0, out waveform.PAPR_dB);  //Version 2.0.0 and later
            }
            NIRfsgPlayback.ReadBurstStartLocationsFromFile(filePath, 0, ref waveform.BurstStartLocations);
            NIRfsgPlayback.ReadBurstStopLocationsFromFile(filePath, 0, ref waveform.BurstStopLocations);

            //Statement reads: if NOT BurstStartLocations > 0 AND expression is not null (? operand)
            //In other words, manually set BurstStartLocations when the length is 0 or less or array is null
            if (!(waveform.BurstStartLocations?.Length > 0))
            {
                //Set burst start to the first sample(0)
                waveform.BurstStartLocations = new int[1] {
                    0
                };
            }
            if (!(waveform.BurstStopLocations?.Length > 0))
            {
                //Set burst stop to the last sample (number of samples minus one)
                waveform.BurstStopLocations = new int[1] {
                    waveform.WaveformData.SampleCount - 1
                };
            }

            NIRfsgPlayback.ReadSampleRateFromFile(filePath, 0, out waveform.SampleRate);
            waveform.BurstLength_s = CalculateWaveformDuration(waveform.BurstStartLocations, waveform.BurstStopLocations, waveform.SampleRate);

            if (normalizeWaveform)
            {
                NormalizeWaveform(waveform);
            }

            return(waveform);
        }
Exemplo n.º 2
0
        /// <summary>Loads a waveform and relevant properties from a TDMS file.</summary>
        /// <param name="filePath">Specifies the absolute path to the .TDMS waveform file on disk.</param>
        /// <param name="waveformName">(Optional) Specifies the name to use to represent the waveform. The file name will be used by default.</param>
        /// <returns>The waveform data and associated properties represented in the Waveform type.</returns>
        public static Waveform LoadWaveformFromTDMS(string filePath, string waveformName = "")
        {
            Waveform waveform = new Waveform();

            if (string.IsNullOrEmpty(waveformName))
            {
                waveformName = Path.GetFileNameWithoutExtension(filePath);
                waveformName = FormatWaveformName(waveformName);
            }

            waveform.Name = waveformName;
            NIRfsgPlayback.ReadWaveformFromFileComplex(filePath, ref waveform.Data);
            NIRfsgPlayback.ReadWaveformFileVersionFromFile(filePath, out string waveformVersion);

            if (waveformVersion == "1.0.0")
            {
                // 1.0.0 waveforms use peak power adjustment = papr + runtime scaling
                // we will scale the waveform and calculate papr and runtime scaling manually
                float peak = ComplexSingle.GetMagnitudes(waveform.Data.GetRawData()).Max();
                waveform.RuntimeScaling = 20.0 * Math.Log10(peak);
                NIRfsgPlayback.ReadPeakPowerAdjustmentFromFile(filePath, 0, out double peakPowerAdjustment);
                waveform.PAPR_dB = peakPowerAdjustment + waveform.RuntimeScaling;

                // scale the waveform to full scale
                WritableBuffer <ComplexSingle> waveformBuffer = waveform.Data.GetWritableBuffer();
                ComplexSingle scale = ComplexSingle.FromPolar(1.0f / peak, 0.0f);
                for (int i = 0; i < waveform.Data.SampleCount; i++)
                {
                    waveformBuffer[i] = waveformBuffer[i] * scale; // multiplication is faster than division
                }
            }
            else
            {
                NIRfsgPlayback.ReadPaprFromFile(filePath, 0, out waveform.PAPR_dB); //Version 2.0.0 and later
                NIRfsgPlayback.ReadRuntimeScalingFromFile(filePath, 0, out waveform.RuntimeScaling);
            }

            NIRfsgPlayback.ReadSampleRateFromFile(filePath, 0, out waveform.SampleRate);
            NIRfsgPlayback.ReadSignalBandwidthFromFile(filePath, 0, out waveform.SignalBandwidth_Hz);
            if (waveform.SignalBandwidth_Hz == 0.0)
            {
                waveform.SignalBandwidth_Hz = 0.8 * waveform.SampleRate;
            }

            NIRfsgPlayback.ReadBurstStartLocationsFromFile(filePath, 0, ref waveform.BurstStartLocations);
            NIRfsgPlayback.ReadBurstStopLocationsFromFile(filePath, 0, ref waveform.BurstStopLocations);
            //Statement reads: if NOT BurstStartLocations > 0 AND expression is not null (? operand)
            //In other words, manually set BurstStartLocations when the length is 0 or less or array is null
            if (!(waveform.BurstStartLocations?.Length > 0))
            {
                waveform.BurstStartLocations = new int[1] {
                    0
                }
            }
            ;                                                    //Set burst start to the first sample(0)
            if (!(waveform.BurstStopLocations?.Length > 0))
            {
                waveform.BurstStopLocations = new int[1] {
                    waveform.Data.SampleCount - 1
                }
            }
            ;                                                                               //Set burst stop to the last sample (number of samples minus one)

            // calculating IdleDurationPresent like this also accounts for tools like wlan sfp that put in burst start and stop locations even if there is no idle time in the waveform
            waveform.IdleDurationPresent = waveform.BurstStopLocations.First() - waveform.BurstStartLocations.First() < waveform.Data.SampleCount - 1;

            waveform.BurstLength_s = CalculateWaveformDuration(waveform.BurstStartLocations, waveform.BurstStopLocations, waveform.SampleRate);

            return(waveform);
        }
Exemplo n.º 3
0
        void LoadWaveforms()
        {
            //Creates folder selection box
            FolderBrowserDialog scriptSelect = new FolderBrowserDialog();

            scriptSelect.Description = "All your TDMS are belong to us";
            try
            {
                scriptSelect.ShowDialog();

                //tdmsFiles contains the path for all files in the directory selected by the user
                string[] tdmsFiles = Directory.GetFiles(scriptSelect.SelectedPath, "*.tdms");

                //Foreach loop parses and loads waveform names into lsvWaveforms and RFSGplayback library
                List <string> sampleRates = new List <string>();
                int           wfmsLoaded  = 0;

                foreach (string tdmsPath in tdmsFiles)
                {
                    //Split file path, find name.
                    string[] pathComps = tdmsPath.Split('\\', '.');
                    //If TDMS file extension found
                    if (pathComps[pathComps.Length - 1] == "tdms")
                    {
                        //Try to load wfm into RFSG playback library, read sample rate, and then add name to waveform box
                        try
                        {
                            NIRfsgPlayback.ReadSampleRateFromFile(tdmsPath, 0, out double sampleRate);
                            sampleRates.Add(sampleRate.ToString());
                            wfmsLoaded++;
                            lsvWaveforms.Items.Add(pathComps[pathComps.Length - 2]);
                            //Adding to list because I don't know how to get items from lsvWaveforms...
                            tdmsWaveformNames.Add(pathComps[pathComps.Length - 2]);
                            tdmsFilePaths.Add(tdmsPath);
                        }
                        catch (Exception er)
                        {
                            //If exception is thrown for invalid TDMS file (error -303804), catch the exception but do nothing
                            if (er.Message.Contains("Error code: -303804"))
                            {
                                //Do nothing
                            }
                            //For any other exception, throw it
                            else
                            {
                                throw (er);
                            }
                        }
                    }
                }


                //Check the number of waveforms loaded
                if (wfmsLoaded == 0)
                {
                    throw new FileNotFoundException("No valid TDMS files found in specified directory.");
                }
                //Check sampleRates for distinct values. If there are more than 1 distinct value, throw exception)
                if (sampleRates.Distinct().Count() > 1)
                {
                    throw new Exception("Different sample rates for each file detected. Ensure all TDMS waveforms use same sample rate.");
                }
                else
                {
                    iqRate = double.Parse(sampleRates[0]);
                }
            }
            catch
            {
                //Show Error
            }
        }