how do I implement a real time FFT in simulink?

I need to implement a real time FFT of the signal in the attached simulink model in order to detect the frequency and phase of the highest peak (it's a single sinusoid, so it shouldn't be an issue). As you can see, I've set the simulation step to 1/48000. I would like the detection update rate to be 40Hz, so the FFT is run 40 times a second. I'd like the FFT to be run on the newest samples (i.e. 48000/40) plus an overlap of 50% of the previous set (48000/80), so in other words, the latest 1800 samples. I'd like to zero pad this up to the next power of 2.
I was trying to figure out how to do a FIFO buffer that saves the last 1800 samples, but I couldn't figure that out, and I couldn't advance past that stage.
In case it helps, here's what I have:
Simulink Version 24.2 (R2024b)
Control System Toolbox Version 24.2 (R2024b)
DSP System Toolbox Version 24.2 (R2024b)
Optimization Toolbox Version 24.2 (R2024b)
Signal Processing Toolbox Version 24.2 (R2024b)
Stateflow Version 24.2 (R2024b)
Statistics and Machine Learning Toolbox Version 24.2 (R2024b)
Thanks in advance
-Bastian

Answers (1)

hello
why not simply use a counter to do the job ? (as we deal with a sinusoidal wave)
you need one counter for the frequency and another one to compute the phase vs a reference signal
doing a full fft for just one single frequency is bit of overkill in my eyes ...
attached a demo file
hope it helps !

7 Comments

Hi. Thanks for the response. In reality, the input signal, called "disturbance" in my model, will have lots of frequency content, so crossing detection won't work. Think of it as a machine with broadband vibration, and there's a shaft that's rotating at a time-varying tone (varies in amplitude and frequency) somewhere between 30 and 80 Hz which should generate a vibration peak in that range. I guess that's one part I didn't specify: find the max amplitude tone only in that frequency range of 30-80Hz. The "disturbance" signal is the signal coming from an accelerometer. I need to detect the shaft tone's frequency, phase and magnitude and update it at 40Hz.
  • well I suppose your vibration signal is related to the shaft rpm , so even if its "broadband" (I suppose you mean it contains harmonics of the rotationnal speed freq) it's periodic by nature , so a counter is still an option .
  • find the max amplitude tone only in that frequency range of 30-80Hz : do you mean you only have to track the shatf freq tone , and remove the harmonics ?
  • there is a FFT block in Simulink , have you tried ?
  • It'll have harmonics, but there'll be lots of other frequency content as well. The shaft is not the only source of vibration. So the crossing detection really will not work.
  • Yes, I only need to detect the shaft frequency, phase and amplitude, not its harmonics.
  • I've tried the FFT block, but I need to have it only perform an FFT on the most recent 1800 samples, which is why I thought a FIFO buffer would work. So every 1/40 of a second, the array from the FIFO buffer is fed into the FFT block, and the algorithm finds the frequency and phase of the highest frequency bin between 30 and 80Hz. I'd like to be able to zero pad too, but that's a bonus.
I noticed there's a Short-Time FFT block, but I wasn't able to make it work. It has fields to specify the analysis window length and the overlap. When I feed the disturbance into x[n] input port I get the following:
Error:Error in 'tachlessSim/Short-Time FFT/Rebuffer': All sample times must be discrete. No continuous or constant sample times are allowed.
Also, I don't really understand what to put in w[n] input port.
No continuous or constant sample times are allowed.
you need to feed a fft block with fixed rate (discrete) signals , so either your entire simulation is running with fixed rate or (if you're using a variable step solver) you have to convert signal to discrete using a ZOH block (look at the different options for rate conversion)
I went to the Configuration Parameters under Model Settings, and changed it to Type: Fixed-step and Solver: Discrete and set the Fixed Step Size (see attachment). Get this error: Error:The "FixedStepDiscrete" solver cannot be used to simulate block diagram 'tachlessSim' because it contains continuous states
However, I'm not worried about that. I would like to know how I implement an 1800 element FIFO buffer and run the FFT on that 40 times a second.
Thanks

Sign in to comment.

Products

Release

R2024b

Tags

Asked:

on 17 Jan 2025

Commented:

on 17 Jan 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!