Multi-channel magnitude spectrum
This sample implements the magnitude spectrum for 5 input channels simultaneously. The code is split into two tasks: a control task, which collects the input samplings of a hardware module, e.g. EL3632, and a CM task, which calculates the spectrum. The block diagram below shows the analysis chain implemented in the program.
The source code for the sample is available for download from here:
Multichannel_FrequencyAnalysis_Sample.zip
Block diagram
Program parameters
The table below shows a list of important parameters for the configuration of the magnitude spectrum function blocks.
Channels | 5 |
FFT-length | 4096 |
Window size | 3200 |
Buffer size | 1600 |
Window type | eCM_HannWindow |
Scaling type | eCM_RootPowerSum |
Conversion to decibels | FALSE |
Global constants
These parameters are defined as constants in the list of global variables.
VAR_GLOBAL CONSTANT
cOversamples : UDINT := 20; // number of oversamples
cMaxChannels : UDINT := 5; // Number of data channels
cWindowType : E_CM_WindowType := E_CM_WindowType.eCM_HannWindow; // window type for analysis
cWindowLength : UDINT := 3200; // length of signal window.
cOverlap : UDINT := 1600; // recommended buffer overlap
cBufferLength : UDINT := cWindowLength - cOverlap; // internal buffer size with 50% overlapping
cFFTLength : UDINT := 4096; // length of FFT for mag. spectrum
cFFTResult : UDINT := 2049; // result of mag. spectrum (cFFTLength/2+1)
END_VAR
Global variables
The parameters are defined in the list of global variables.
VAR_GLOBAL
bInvalidateCh4 : BOOL := FALSE; // Invalidate input signal on channel 4
END_VAR
Explanations
The result of the sample code can be tested for a sinusoidal signal of any amplitude and frequency. The RMS values are saved in the array aRmsValue
in accordance with the respective channel number. The result must correspond exactly to the peak amplitude of every sinusoidal signal divided by SQRT(2)
. The sample code can be extended for more than 5 channels depending on the requirements and the resources of the target system.
The setting of the global variables bInvalidateCh4 := TRUE
demonstrates a possible error handling with impermissible input data.
Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT v3.1.4022.25 | PC or CX (x86, x64) | Tc3_CM, Tc3_CM_Base, Tc3_MultiArray |