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:
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
However, the one from the matlab code is like the following:
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
2- To workspace block
3- Solver
go to model configuration parameter and select Data Import/Export. Untick the Single simulation output and click on Apply. double tap the scope and go to Logging and select Log data to the workspace and select Structure with Time and click on Apply. double tap Powergui and select FFT Analysis.
you can find the "To Workspace" in simulink library. Just connect to the graph which you wan to plot. After that go to command window and type Plot( file name of To workspace) and enter you will find the graph .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With