MATLAB: Calculating the amplitude of a sine wave in simulink

amplitudepeaksimulinksine wavestore data

I am trying to create a feedback control loop that will give me a constant amplitude of a sine wave for any frequency. The sine wave is being generated by an external sensor and is an input into my control signal which will then calculate the correct propotional gain to give the constant amplitude I specify. I am trying to find a method of calculating the current peak (this all needs to be done in real time) of the sine wave? So far I have only been able to calculate each position of the sine wave while it goes through one wave length. Can someone tell me how I can store all of this data in Simulink (i.e it cannot go out to the MATLAB workspace) so that I can find the maximum value. Or is there another better way to do this?

Best Answer

There is a block in the Math Operations library called 'MinMax Running Resettable' that will do exactly what you would like. Connect the sine wave to the first port and you'll have to create another signal to reset the block every period (or several periods). One approach to creating the reset signal might be to use the 'Detect Rise Positive' block from the Logic and Bit Operations library on the sine wave. Here's an example model: