From SIMULINK to workspace FFT?
Asked Answered
T

2

6

If I want to plot the PSD of a simple sinusoidal wave 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 sinewave generator in SIMULINK and I need to plot the PSD. I did something like the following: enter image description here

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

Please note that the variable Sinewave is 1x1 double time series. I tried to just replace x with Sinwave, but it didn't work.

Update: Applying the answers

I applied what's recommended down, but I have a different output than if I do 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 for the SIMULINK-exported sinewave enter image description here

However, the one from the matlab code is like the following: enter image description here

I need the output to be like this from matalb with all these ripples in the noise floor. How can I get the exact output?

Please note I've used the exact values for both.

Update 2: SIMULINK sinewave setup

1- Sinewave block

enter image description here

2- To workspace block

enter image description here

3- Solver

enter image description here

Tjirebon answered 20/9, 2016 at 15:30 Comment(8)
Check your sampling frequency in the Sine wave block. Plot both data on the same figure and see where they are different. You may want to post your update as a different question since it is about the sine wave data and not about logging to workspace.Anarchic
1- I would say it's the same questions. Because it's about correctly exporting data to Matlab. 2- I don't need to plot on the same figure because the differences are quit big and really obvious. You only plot on same figure if differences are small. 3- Sampling frequency and everything is exactly the same as mentioned before.Tjirebon
I get the same result as Simulink by using the exact code you also have. Did you set the Simulink solver to fixed-timestep and the correct sampling frequency?Aconcagua
That's weird, because I set yes the solver step-size to fixed 1e-3 which is equal to the sample freq Fs. Also, I tried to change the solver step-size to 1e-4, 1e-5 but still same output. What are we possibly overlooking here?Tjirebon
I meant the sine wave data to be plotted on the same figure. Not psd.Anarchic
@Tjirebon if your problem persists, can you please show us the setup of your sine wave source?Aconcagua
Update done. ThanksTjirebon
@Tjirebon see my edited answer - does this solve your problem?Aconcagua
A
4

If you take a close look at your Sinewave variable, i.e. by simply typing its name in the command prompt, 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, e.g. Time and Data, which are both arrays. We could simply try plotting these for example:

plot(Sinwave.Time, Sinwave.Data)

And indeed, this gives us a nice plot of your sine wave. We could now try replacing the variable t with Sinewave.Time and x with Sinewave.Data, which allows us to plot the PSD.

If you don't know the sampling frequency, you can use the Sinewave.TimeInfo - which contains the number of samples, and the start and end time, to calculate the sampling frequency Fs.


Responding to your edited question: the time vector t, which you create with your MATLAB code, is 0 : 0.001 : 0.999 and is of length 1000. Your Simulink simulation, however, runs from t=0 to t=1 in steps of 0.001, thus your resulting time- and data-vectors are of length 1001! The calculation assumes that the step size is 1/1001 instead of 1/1000, leading to different results. To solve that problem, change the Stop Time in the simulation setup to 0.999. Then, the resulting vectors are of correct size, and you get the same result as the MATLAB calculation.

resulting plot

Aconcagua answered 20/9, 2016 at 15:58 Comment(1)
Thanks for the answer. But please check the update as the output is not the same.Tjirebon
A
2

Timeseries object contains a Data field which should contain the sinewave data. You can do x = Sinwave.Data; and can then use the rest of your code. Alternatively you can also set the "Save format" property of "To workspace" block to "Array". This will make Sinwave a regular MATLAB array. You can then simply replace x with Sinwave.

Anarchic answered 20/9, 2016 at 15:56 Comment(1)
Kindly check the update. I still have a problem with the output.Tjirebon

© 2022 - 2024 — McMap. All rights reserved.