日本語
English
Steps for FFT Analysis of Analog Input and Graph Display (2/5)
Sampling
Here the sampling frequency is set to 10 kHz so frequencies up to 5 kHz can be observed
The number of samples is 4096, sampling for about 0.5 seconds
Also, a filter is applied to the analog input as needed; here we use a high-pass filter to cut frequencies below 1 kHz before calculating the FFT
Procedure
- Step 1: High-Speed Analog Unit Settings
- 1-1. Add the unit
- 1-2. Set the number of samples
- 1-3. Edit I/O allocation settings
- 1-4. Edit the I/O map
- Step 2: Filter the Analog Values and Put Them in an Array
- 2-1. Capture the analog values
- 2-2. Apply the high-pass filter
- Step 3: FFT Calculation
- 3-1. Save analog values to the FFT array
- 3-2. Add a program
- 3-3. Add an event task
- 3-4. Program assignment settings
- 3-5. Start the event task
- 3-6. Check operation
- 3-7. Check execution time
Step 1: High-Speed Analog Unit Settings
1-1. Add the unit
1-2. Set the number of samples
1-3. Edit I/O allocation settings
Open [Edit I/O Allocation Settings]; if you keep the default task period of 2 ms,
number of samples = 20
sampling period = 100 us
sampling frequency = 10 kHz
Check these, press the [Update I/O Entries] button, press [Apply] and then the [OK] button
number of samples = 20
sampling period = 100 us
sampling frequency = 10 kHz
Check these, press the [Update I/O Entries] button, press [Apply] and then the [OK] button
1-4. Edit the I/O map
Open the I/O map, right-click "Ch1 Analog1 Input Value 1-10" and "Ch1 Analog1 Input Value 11-20" and run Create Device Variable
Step 2: Filter the Analog Values and Put Them in an Array
2-1. Capture the analog values
Create the internal variable AnalogInputData[20]
and store N1_Ch1_Analog_Input_Value_1_10 and N1_Ch1_Analog_Input_Value_11_20 into AnalogInputData while casting (converting) them to REAL
and store N1_Ch1_Analog_Input_Value_1_10 and N1_Ch1_Analog_Input_Value_11_20 into AnalogInputData while casting (converting) them to REAL
2-2. Apply the high-pass filter
Copy the HPF function into Functions from the analog filter sample
Next, create the following internal variables
cnt UINT
buf REAL[4]
AnalogFileteInput LREAL[20]
Pass the analog input data put into the array in 2-1 through the high-pass filter from the start and store it in the AanalogFilterInput array
The sampling frequency is 10 kHz and the cutoff frequency is 100 Hz
Next, create the following internal variables
cnt UINT
buf REAL[4]
AnalogFileteInput LREAL[20]
Pass the analog input data put into the array in 2-1 through the high-pass filter from the start and store it in the AanalogFilterInput array
The sampling frequency is 10 kHz and the cutoff frequency is 100 Hz
Step 3: FFT Calculation
3-1. Save analog values to the FFT array
Create the global variable FFTData[4096] and save the data for FFT calculation there
When the FFTStart flag turns ON, it self-holds, and the FFTRun flag is ON while data is being saved
During FFTRun, the 20 analog values acquired per scan are put into the FFTData array in order
When FFTDataCnt reaches 4096, the data is ready, so FFTDataSetFlg turns ON and the FOR loop exits
When the FFTStart flag turns ON, it self-holds, and the FFTRun flag is ON while data is being saved
During FFTRun, the 20 analog values acquired per scan are put into the FFTData array in order
When FFTDataCnt reaches 4096, the data is ready, so FFTDataSetFlg turns ON and the FOR loop exits
3-2. Add a program
Copy the FFT function into Functions from the FFT sample
FFT takes time to calculate, so add a program for an event task to perform the FFT calculation (Program1 here)
Event tasks run using the idle time of the primary task and periodic tasks, so the global variable CalcFin is turned ON to signal that the FFT calculation is complete
FFT takes time to calculate, so add a program for an event task to perform the FFT calculation (Program1 here)
Event tasks run using the idle time of the primary task and periodic tasks, so the global variable CalcFin is turned ON to signal that the FFT calculation is complete
3-3. Add an event task
In Task Settings, add a task with the [+] button and select Event task (priority 8) or (priority 48) as the task type
3-4. Program assignment settings
In Program Assignment Settings, press [+] for EventTask0 and set the added program (Program1)
3-5. Start the event task
In the main program (Program1), when FFTDataSetFlg turns ON the data is complete, so the event task is started with ActEventTask
While the event task is running, the self-holding circuit holds, so FFTRun stays ON
When the FFT calculation in the event task finishes, CalcFin turns ON, so FFTEnd turns ON and releases the self-hold
Do not forget to turn the CalcFin flag OFF
The PowerSpectrumData array has 8192 elements, but because the FFT input data is 4096 values, the FFT results are in half of that, 2048 elements
Ignore data from 2048 onward
While the event task is running, the self-holding circuit holds, so FFTRun stays ON
When the FFT calculation in the event task finishes, CalcFin turns ON, so FFTEnd turns ON and releases the self-hold
Do not forget to turn the CalcFin flag OFF
The PowerSpectrumData array has 8192 elements, but because the FFT input data is 4096 values, the FFT results are in half of that, 2048 elements
Ignore data from 2048 onward
3-6. Check operation
The program is complete, so check its operation
Turning ON FFTStart in Program0 starts the FFT calculation; FFTRun is ON during calculation, and when it completes FFTEnd turns ON and FFTRun turns OFF
Now the FFT calculation data has been obtained
Since 4096 data points were taken at 10 kHz, 2048 data points from 0 to 5 kHz were produced, so the array contains data with a resolution of 5 kHz / 2048 = 2.44 Hz
Turning ON FFTStart in Program0 starts the FFT calculation; FFTRun is ON during calculation, and when it completes FFTEnd turns ON and FFTRun turns OFF
Now the FFT calculation data has been obtained
Since 4096 data points were taken at 10 kHz, 2048 data points from 0 to 5 kHz were produced, so the array contains data with a resolution of 5 kHz / 2048 = 2.44 Hz
3-7. Check execution time
Confirm that the task execution time of the primary task is not affected by the FFT calculation
Incidentally, in the sample the execution time of EventTask0, which performs the FFT calculation, is 11.649 ms
Incidentally, in the sample the execution time of EventTask0, which performs the FFT calculation, is 11.649 ms
On the next page
The FFT results are now in the PowerSpectrumData array
On the next page, this data is graphed on an HMI so it is easy to understand
