Crest factor
This sample calculates the crest factor for an input signal. Although the function block FB_CMA_CrestFactor is able to process several channels, for the purpose of illustration only a single channel will be considered. The block diagram below shows the analysis chain implemented in the program.
The source code for the sample is available for download from here:
CrestFactor_Sample.zip
Block Diagram
Program parameters
The table below shows a list with important configuration parameters for the function block for calculating the crest factor.
Channels | 2 |
Buffer size | 1600 |
Global constants
These parameters are defined in the global variable list as constants.
VAR_GLOBAL CONSTANT
cOversamples : UDINT := 10; // oversampling factor
cChannels : UDINT := 2; // number of channels
cBufferLength : UDINT := 2000; // size of buffer
END_VAR
Code for Control Task
Following code snippet shows the declaration in MAIN program:
VAR CONSTANT
cInitSource : ST_MA_MultiArray_InitPars := ( eTypeCode := eMA_TypeCode_LREAL, nDims := 2, aDimSizes := [cChannels, cBufferLength]);
END_VAR
VAR
nInputSelection : UDINT := 1;
aCrestFactor : ARRAY[1..cChannels] OF LREAL;
nSampleIdx : UDINT;
nChannelIdx : UDINT;
aEl3632 AT %I* : ARRAY[1..cChannels] OF ARRAY[1..cOversamples] OF INT; // input from hardware e.g. EL3632
aBuffer : ARRAY[1..cChannels] OF ARRAY[1..cOversamples] OF LREAL;
fbSource : FB_CMA_Source := (stInitPars := cInitSource, nOwnID := eID_Source, aDestIDs := [eID_Crest]); // Initialize source
fbSink : FB_CMA_Sink := (nOwnID := eID_Sink);
END_VAR
Method calls in Main program:
// Collect data in a source
fbSource.Input2D(pDataIn := ADR(aBuffer),
nDataInSize := SIZEOF(aBuffer),
eElementType := eMA_TypeCode_LREAL,
nWorkDim0 := 0,
nWorkDim1 := 1,
pStartIndex := 0,
nOptionPars := 0 );
// Push results to sink
fbSink.Output1D(pDataOut := ADR(aCrestFactor),
nDataOutSize := SIZEOF(aCrestFactor),
eElementType := eMA_TypeCode_LREAL,
nWorkDim := 0,
nElements := 0,
pStartIndex := 0,
nOptionPars := 0,
bNewResult => bNewResult);
Code for CM Task
Declaration in MAIN_CM program:
VAR CONSTANT
cInitCrest : ST_CM_CrestFactor_InitPars := ( nChannels := cChannels, nBufferLength := cBufferLength );
END_VAR
VAR
fbCrest : FB_CMA_CrestFactor := (stInitPars := cInitCrest, nOwnID:= eID_Crest, aDestIDs:= [eID_Sink]); // Initialize crest
END_VAR
Method calls in MAIN_CM program:
fbCrest.Call();
The result of the sample code can be checked for a sinusoidal signal of arbitrary amplitude and frequency as the input signal. The crest factor, in this case first element of aCrestFactor, must be equal to 3.01 dB.
Requirements
Development environment |
Target system type |
PLC libraries to include |
---|---|---|
TwinCAT v3.1.4013 |
PC or CX (x86, x64) |
Tc3_CM, Tc3_CM_Base, Tc3_MultiArray |