ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

RealFFT problem

Hi guys:

            I used measurement studio  2015. Sampling from ADC to get 1024  data(singals: 30~100HZ 1~5Vpp sin wave) ,saved to excel. imported  from excel to make FFT. according to the filter examples .

codes as following:

void CalculateFFTFunction(double []waveform)
{
//waveform data size
int datasize = waveform.Length;

//number of samples for FFT data
int fftnumofSamples = datasize/2;
xwaveform = new double[fftnumofSamples];
magnitudes = new double[datasize];
subsetOfMagnitudes = new double[fftnumofSamples];
phases = new double[datasize];
subsetOfPhases = new double[fftnumofSamples];
logMagnitudes = new double[fftnumofSamples];
FFTValue = new ComplexDouble[datasize];
int i;

try
{
// Calculate the FFT of waveform array.
FFTValue = NationalInstruments.Analysis.Dsp.Transforms.RealFft(waveform);

// Get the magnitudes and phases of FFT array..
NationalInstruments.ComplexDouble.DecomposeArrayPolar(FFTValue, out magnitudes, out phases);

double scalingFactor = 1.0/(double)datasize;

double deltaFreq = samplingRateNumericEdit.Value * scalingFactor;
textEdit1.EditValue = samplingRateNumericEdit.Value;
double maxvalue= new double();
double imaxvalue=new double();


subsetOfMagnitudes[0] = magnitudes[0] * scalingFactor;

// It's sufficient to plot just the half of numberOfSamples points to show the FFT.
// Because the other half will be just the mirror image of the first half.
for(i=1; i<fftnumofSamples; i++)
{
// Generating xwaveform with respect to which magnitude and phase will be plotted.
xwaveform[i] = deltaFreq * i;
subsetOfMagnitudes[i] = magnitudes[i]*scalingFactor*Math.Sqrt(2.0); // Storing only half the magnitudes array.
maxvalue = ArrayOperation.GetMax(subsetOfMagnitudes);
imaxvalue = ArrayOperation.GetIndexOfMax(subsetOfMagnitudes);

subsetOfPhases[i] = phases[i]; // Storing only half of the phases array.
}

// Display mode: linear or exponential
switch(displayModeComboBox.SelectedIndex)
{
// Plot the magnitudes and the phases.
default:
case 0: // Linear mode.
magnitudePlot.YAxis.Caption = "Magnitude VRMS";
magnitudePlot.PlotXY(xwaveform, subsetOfMagnitudes);
phasePlot.PlotXY(xwaveform, subsetOfPhases);
break;
case 1: // Exponential mode.
for(i=0; i<fftnumofSamples; i++)
{
logMagnitudes[i] = 20.0*System.Math.Log10(magnitudes[i]);
}
magnitudePlot.YAxis.Caption = "Magnitude in dB";
magnitudePlot.PlotXY(xwaveform, logMagnitudes);
phasePlot.PlotXY(xwaveform, subsetOfPhases);
break;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}

Attached the wave, signal is 80hz 1vpp Sinwave  

but i found the magnitude and frequcney valule  are incorrect.  Is there anyone who can give me some advice? Thank you for your advice.

Download All
0 Kudos
Message 1 of 2
(2,884 Views)
samplingRateNumericEdit.Value=500; 
0 Kudos
Message 2 of 2
(2,876 Views)