From SIMULINK to the FFT workspace?

If I want to build a simple sine wave PSD in Matlab, I would do something like the following:

Fs = 1000; t = 0:1/Fs:1-(1/Fs); x = cos(2*pi*100*t) ; N = length(x); xdft = fft(x); xdft = xdft(1:N/2+1); psdx = (1/(Fs*N)) * abs(xdft).^2; psdx(2:end-1) = 2*psdx(2:end-1); freq = 0:Fs/length(x):Fs/2; plot(freq,10*log10(psdx)) grid on 

But suppose for simplicity I have a sine wave generator in SIMULINK and I need to build a PSD. I did something like the following: enter image description here

Then I got a variable called "Sinwave", how can I apply the above Matlab code to build a PSD?

Note that the Sinewave variable is a 1x1 double time series. I tried just replacing x with Sinwave, but that didn't work.

Update: Applying Responses

I applied what I recommended, but I have a different result than if I did it with Matalb. Here is the code I used:

 Fs = 1000; x = Sinwave.Data; N = length(x); dft = fft(x); dft = dft(1:N/2+1); psd = (1/(Fs*N)) * abs(dft).^2; psd(2:end-1) = 2*psd(2:end-1); freq = 0:Fs/length(x):Fs/2; plot(freq,10*log10(psd)) grid on 

This one is for SIMULINK-exported sine wave enter image description here

However, one of the matlab code looks like this: enter image description here

I need the result to be the same as matalb , with all these ripples at the noise level . How can I get the exact result?

Please note that I used exact values ​​for both.

Update 2: setting the sine wave SIMULINK

1- Sine Wave Block

enter image description here

2- In workspace unit

enter image description here

3- Solver

enter image description here

+6
source share
2 answers

If you look closely at your Sinewave variable, i.e. just typing her name on the command line, you will see the following:

  >> Sinwave timeseries Common Properties: Name: '' Time: [51x1 double] TimeInfo: [1x1 tsdata.timemetadata] Data: [51x1 double] DataInfo: [1x1 tsdata.datametadata] More properties, Methods 

It contains different fields, for example. Time and Data , which are both arrays. We could just try to build them, for example:

 plot(Sinwave.Time, Sinwave.Data) 

And indeed, it gives us a good plot of your sinusoid. Now we can try to replace the variable t with Sinewave.Time and x with Sinewave.Data , which allows us to build a PSD.

If you do not know the sampling rate, you can use Sinewave.TimeInfo - which contains the number of samples and the start and end times to calculate the sampling frequency Fs .


Answering your edited question: the time vector t that you create using the MATLAB code is 0 : 0.001 : 0.999 and has a length of 1000. However, the Simulink simulation works from t=0 to t=1 in steps 0.001 , so your resulting time vectors and the data has a length of 1001! The calculation assumes that the step size is 1/1001 instead of 1/1000 , which leads to different results. To solve this problem, change the Stop Time value in the simulation setup to 0.999. Then the resulting vectors are of the correct size, and you will get the same result as the MATLAB calculation.

final schedule

+4
source

The Timeseries object contains a data field that should contain sine wave data. You can do x = Sinwave.Data; and then use the rest of the code. Alternatively, you can also set the Save Format property of the To Workspace block to Array. This will make Sinwave a regular MATLAB array. Then you can simply replace x with Sinwave.

+2
source

All Articles