Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Managing and editing AnalogWaveform

I am using an AnalogWaveform<double> to store a waveform of recorded samples.

 

I want to run my waveform through an FIR filter to clean it up a little. Here is the function I wrote:

 

 public static AnalogWaveform<double> FIRFilter(AnalogWaveform<double> Waveform, double [] Coefficients)
        {
            
            AnalogWaveform<double> Result;
            
            NationalInstruments.Analysis.Dsp.Filters.FirDirectFilter MyFilter = new FirDirectFilter(Coefficients);
            double[] InputData = Waveform.GetRawData();
            double[] OutputData = MyFilter.FilterData(InputData);
            Result = new AnalogWaveform<double>(0, Waveform.Capacity);
            Result.Append(OutputData);
            Result.Timing = Waveform.Timing; // I get an exception here, telling me that "The number of irregular time stamps is not equal to the number of samples in the waveform."   
            
            return Result;
        }

 

I was expecting that I would have something like:

 

Waveform.Samples = { { 0, 7/16/13 9:22:22.01},  { 0.002, 7/16/13 9:22:22.01}, .....  { 0.3223, 7/16/13  9:22:28.93} }

 

After the filtering and append I was expecting:

Result.Samples = { { Filtered0},  { Filtered 0.002}, .....  { Filtered 0.3223} }

and after the timing:

Result.Samples = { { Filtered 0, 7/16/13 9:22:22.01},  { Filtered 0.002, 7/16/13 9:22:22.01}, .....  { Filtered 0.3223, 7/16/13  9:22:28.93} }

 

Can you tell me why I am getting this exception and the "right way" to get what I want? 

 

2) This is my construction of that waveform:


            _RawSampleWaveform = new AnalogWaveform<double>(0, 0);
            _RawSampleWaveform.Append(new double[] { 0 });
            _RawSampleWaveform.Timing = WaveformTiming.CreateWithIrregularInterval(new DateTime[] { DateTime.Now });


Is there a better way than having a 'fake' 0 sample. If I try to change the timing without that 'fake' sample I get the same exception-- that my number of samples does not match my number of timestamps.

 

Suggestions on the "right" way to do this appreciated.

 

Thanks,


Roger

0 Kudos
Message 1 of 4
(5,607 Views)

Hi Roger,

 

It appears you created a service request with this issue. I will be following up to that email to troubleshoot further and then, once we come to some good resolutions, post the updates on this forum.


Others are still free to give thoughts and advice in this forum as they would desire in the meantime.

 

Regards,

James W.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 4
(5,565 Views)

After investigation, it appears the issue is due to a bug with the Clone functionality of AnalogWaveform not making a proper deep copy of the Timing data, which would then throw the cloned data out of sync as the original continues to generate information.

 

Corrective Action Request #418784 has been filed in response.

 

Regards,

 

James W.
Applications Engineer
National Instruments
0 Kudos
Message 3 of 4
(5,533 Views)

Thanks James.

 

Here is the code which reproduced the issue (VS2010, MS2012 for VS2010):

 

namespace Request7386081AnalogWaveformHelp
{
    public partial class MainForm : Form
    {
        AnalogWaveformGeneratorClass WaveformGen;
        Thread WaveformGenThread;
        System.Windows.Forms.Timer RefreshTimer;
        double[] Coefficients;

        public MainForm()
        {
            InitializeComponent();
            RefreshTimer = new System.Windows.Forms.Timer();
            RefreshTimer.Interval = 100;
            RefreshTimer.Tick += new EventHandler(RefreshTimer_Event);
            WaveformGen = new AnalogWaveformGeneratorClass();
            WaveformGenThread = new Thread(new ThreadStart(WaveformGen.GeneratorDaemon));
            WaveformGenThread.Start();
            RefreshTimer.Enabled = true;
            Coefficients = new double[200];
            for (int i = 0; i < 200; i++) { if (i == 199) Coefficients[i] = 1; else Coefficients[i] = 0; } // Just move everything back 2 seconds.

        }
        private void RefreshTimer_Event(object o, EventArgs ea)
        {
            AnalogWaveform<double> CloneOfWaveform = WaveformGen.Waveform.Clone();  // Per the Solution offered, this clone is not working right.

                                                                                                                  
            AnalogWaveform<double> Filtered = AnalogWaveformGeneratorClass.Filter(CloneOfWaveform,Coefficients);
        }
    }
}

 

 

namespace Request7386081AnalogWaveformHelp
{
    class AnalogWaveformGeneratorClass
    {
        
        public AnalogWaveform<double> Waveform;
        public AnalogWaveformGeneratorClass()
        {
            Waveform = new AnalogWaveform<double>(0);
            Waveform.Append(new double[] { 0 });
            Waveform.Timing = WaveformTiming.CreateWithIrregularInterval(new DateTime[]{DateTime.Now});
        }
        public double GeneratorFunction(double Seconds)
        {
            Seconds %= 10;
            if ((Seconds > 1) && (Seconds < 2)) return 5;
            return 0;
        }
        public void GeneratorDaemon()
        {
            DateTime TimeIStarted = DateTime.Now;
            DateTime TimeOfNextSample = DateTime.Now;
            while (true)
            {

               // Yes I know I should be doing this with locks but it wasn't relevant to the issue. 🙂
                Waveform.Append(new double[] { GeneratorFunction((DateTime.Now - TimeIStarted).TotalSeconds) }, new DateTime[] {DateTime.Now});
                TimeOfNextSample += new TimeSpan(0, 0, 0, 0, 10);
                TimeSpan TimeToSleep = TimeOfNextSample - DateTime.Now;
                if (TimeToSleep.TotalSeconds < 0) { TimeToSleep = new TimeSpan(0, 0, 0, 0, 0); }
                Thread.Sleep(TimeToSleep);
            }

        }

        public static AnalogWaveform<double> Filter(AnalogWaveform<double> OriginalWaveform,double [] Coefficients)
        {
            AnalogWaveform<double> Result = new AnalogWaveform<double>(0);

            NationalInstruments.Analysis.Dsp.Filters.FirDirectFilter MyFilter = new FirDirectFilter(Coefficients);
            double[] InputData = OriginalWaveform.GetRawData();
            double[] OutputData = MyFilter.FilterData(InputData);
            Result = new AnalogWaveform<double>(0, OriginalWaveform.Capacity);
            Result.Append(OutputData);
            Result.Timing = OriginalWaveform.Timing; // I get an exception here, telling me that "The number of irregular time stamps is not equal to the number of samples in the waveform."  
            return Result;
        }
    }
}

 

 

0 Kudos
Message 4 of 4
(5,531 Views)