From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Plotting AnalogWaveformCollection<double> data onto the WaveformGraph in Windows Form

Solved!
Go to solution

Hi Everyone,

 

I have list of values of type AnalogWaveformCollection<double> and I wish to plot these values onto a graph. According to some sources from the website I used the following method inorder to do but I get an error which says :  Type arguments cannot be inferred from the usage. Try specifying them explicitly.

 

Here is the code:

 

void StartAcquisition()
        {
           
            PrecisionTimeSpan timeout = new PrecisionTimeSpan(5.0);
            AnalogWaveformCollection<double> waveforms = null;
            try
            {
                InitializeSession();
                scopeSession.Measurement.AutoSetup();

                long recordLength = scopeSession.Acquisition.RecordLength;
                double sampleRate = scopeSession.Acquisition.SampleRate;

                waveforms = scopeSession.Channels[ChannelName].Measurement.Read(timeout, recordLength, waveforms);

                //waveformGraph1.PlotWaveforms(waveforms);  // error
            }

Can anyone please  help ?

 

Regards,

Priyanka

0 Kudos
Message 1 of 16
(6,273 Views)

Hi, Priyanka

 

Can you help us understand your code better? As far as I understand, your code should not work for a different reason: PlotWaveforms expects an array of AnalogWaveform<T> objects, and you're providing it with an AnalogWaveformCollection<T>. I wrote a quick snippet to illustrate my point better:

 

// Can be plotted using PlotWaveform(s).
var singleWaveform = AnalogWaveform<double>.FromArray1D(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 4.0, 3.0, 2.0, 1.0 });
var waveformArray = new AnalogWaveform<double>[] { singleWaveform, singleWaveform };

// Cannot be plotted using PlotWaveform(s).
var waveformCollection = new AnalogWaveformCollection<double>();

_waveformGraph.PlotWaveform<double>(singleWaveform);
_waveformGraph.PlotWaveforms<double>(waveformArray);

// This shouldn't work because PlotWaveforms expects an array of AnalogWaveform<T> objects.
// _waveformGraph.PlotWaveforms<double>(waveformCollection);

Can you post an image of the exception you're seeing?


Thanks

Daniel Dorroh
National Instruments
Message 3 of 16
(6,167 Views)

Hi,

 

Thank you Daniel. As per your suggestion, I have made sure that I plot AnalogWaveform<double> data. Here is the error free code:

 


        static void PlotWaveformsOnGraph(AnalogWaveformCollection<double> waveforms)
        {
            AnalogWaveform<double> SampledWaves = null ;
            double[] waves = new double[waveforms[0].SampleCount];
            int count = 0;
            for (int row = 0; row < waveforms[0].SampleCount; row++)
            {                
                foreach (AnalogWaveform<double> waveform in waveforms)
                {

                    waves[count] = waveform.Samples[row].Value;
                    count++;
                }
            }

            MainWindow main = new MainWindow();
            SampledWaves = AnalogWaveform<double>.FromArray1D(waves);
            main.waveformGraph.PlotWaveform<double>(SampledWaves);
        }

 

 

But , even after successful execution, I do not see the Waveform plotted on the graph. The graph is empty without any waves. What could be the reason.? Any suggestions?

0 Kudos
Message 4 of 16
(6,141 Views)

Hi, Priyanka

 

I think you can do what you are trying to do in a simpler way. Please see the following snippet:

 

void PlotWaveformsOnGraph(AnalogWaveformCollection<double> waveforms)
{
    List<AnalogWaveform<double>> waveformsList = new List<AnalogWaveform<double>>(waveforms);
    waveformGraph.PlotWaveforms(waveformsList.ToArray());
}
Daniel Dorroh
National Instruments
Message 5 of 16
(6,126 Views)

Awesome !! thank you for making it compact. But I still don see the graph on screen. Ideally when I click on "Start Analysis" Button, the waveform should appear in the area. Please refer to the screenshot attached with this message. I really appreciate your time and inputs. Please help me with this.

0 Kudos
Message 6 of 16
(6,084 Views)
If you put a breakpoint at the start of the PlotWaveformsOnGraph function, does the waveforms parameter contain waveforms? How do you know the data is making it to the PlotWaveforms method?
Daniel Dorroh
National Instruments
0 Kudos
Message 7 of 16
(6,076 Views)

Yes, Daniel. I also verified it by printing the values onto the console and I did get the values :

 

List<AnalogWaveform<double>> waveformList = new List <AnalogWaveform<double>>(waveforms);

MainWindow main = new MainWindow();

main.waveformGraph.PlotWaveforms(waveformList.ToArray());

for (int row = 0; row < waveforms[0].SampleCount; row++)

{

       foreach (AnalogWaveform<double> wave in waveformList)

                {

            Console.WriteLine("samplessss"+wave.Samples[row].Value.ToString());

               }

}

 

output:

 

samplessss---->1.48847971245414

samplessss---->1.49389002792304

samplessss---->1.49253744905582

samplessss---->1.4918611596222

samplessss---->1.49118487018859

samplessss---->1.48780342302052

samplessss---->1.4864508441533

samplessss---->1.49118487018859

samplessss---->1.49389002792304

samplessss---->1.48847971245414

samplessss---->1.48983229132136

samplessss---->1.48983229132136

samplessss---->-1.51627424109029

samplessss---->-1.51221650448861

samplessss---->-1.50612989958609

samplessss---->-1.50951134675415

samplessss---->-1.51086392562138

samplessss---->-1.51356908335583

samplessss---->-1.51289279392222

samplessss---->-1.51356908335583

samplessss---->-1.51221650448861

samplessss---->-1.51289279392222

samplessss---->-1.51018763618777

samplessss---->-1.51356908335583

 

n so on......

 

I don't know why its not plotting it on the graph.

 

0 Kudos
Message 8 of 16
(6,062 Views)
How do you have your graph configured? Can you post any code you've written that configures the graph and the contents of InitializeComponent? Are you doing any plot configuration in code aside from PlotWaveforms?
Daniel Dorroh
National Instruments
0 Kudos
Message 9 of 16
(6,049 Views)

Yes Daniel,

//some code

sampledWaves.ScaleMode = WaveformScaleMode.CreateLinearMode(2, 0);

main.xAxis.MajorDivisions.LabelFormat = new FormatString(FormatStringMode.Numeric, "G5");

sampledWaves.Timing = WaveformTiming.CreateWithRegularInterval(main.waveformPlot.DefaultTiming.SampleInterval);

main.waveformGraph.PlotWaveform(sampledWaves, plotOptions);

-------------

public AnalogWaveformPlotOptions initialPlotConfiguration(){

plotOptions = new AnalogWaveformPlotOptions(AnalogWaveformPlotDisplayMode.Samples, AnalogWaveformPlotScaleMode.Raw);

return plotOptions;

}

public AnalogWaveform<double> PlotWaveform(AnalogWaveformCollection<double> waveforms)

{

 double[] waves = new double[waveforms[0].SampleCount];int count = 0;

for (int row = 0; row < waveforms[0].SampleCount; row++)

{

foreach (AnalogWaveform<double> waveform in waveforms)

{

waves[count] = waveform.Samples[row].Value;

count++;

}}

AnalogWaveform<double> SampledWaves = AnalogWaveform<double>.FromArray1D(waves);

SampledWaves.ScaleMode = WaveformScaleMode.CreateLinearMode(2, 0); 

return SampledWaves;

}

 

0 Kudos
Message 10 of 16
(6,002 Views)