Simulink 'From Workspace' and 'To workspace' elements

4 views (last 30 days)
Hello,
I would like to ask a question regarding Simulink. I used the following script to create a sinewave:
Nfft=8192; transient=100;
sampling_freq=22050;
tone_bin_fir=16;
t=[0:(Nfft+transient-1)]/(sampling_freq);
ampl=1;
u1=(10.^(-1/20))*ampl*sin(2*pi*((tone_bin_fir*sampling_freq)/(Nfft))*t);
The sinewave has -1 dB amplitude and sampling frequency 22.05 kHz. The frequency of the input signal is chosen to be in the 16th bin of 8192 point FFT. After the sinewave is created I form a structure with time using the sinewave:
u_struct.time=t';
u_struct.signals.values=u1';
u_struct.signals.dimensions=1;
This structure is used in the ’From Workspace’ element in Simulink schematic. The Simulink schematic is very simple. There are only two elements. One of the elements is the ’From Workspace’ and the other element is ’To Workspace’. The output of ’From Workspace’ is directly connected to the input of ’To Workspace’. Therefore I expect the input vector and output vector to be the same. And this is also the case.
However when I change the sampling_freq from 22050 to 23400 suddenly the output vector is not completely the same as the input vector. My question is - why is this happening? And how can I have the input and output vectors to be the same no matter what the sampling frequency (or any oher parameter of the input sinewave) is?
The settings in Simulink are following:
From Worspace
Data: u_struct;
Sample time: 1/sampling_freq;
Form output afer final data value by: Setting to zero;
To Worspace
Variable name: inx;
Limit datapoints to last: inf;
Decimation: 1;
Sample time: -1;
Save format: Structure with time;
Configuration parameters:
Start time: 0;
Stop time: (16484-1)/sampling_freq;
Type: Fixed-step;
Solver: discrete (no continuous steps);
Fixed-step size: 1/sampling_freq;
Periodic sample time constraint: Unconstrained;
Tasking mode for periodic sample times: Auto;
The script I use to compare the input and output vector is following:
for k=1:1:8242
if inx.signals.values(k)==u_struct.signals.values(k)
vector_comparison(k)=1;
else
vector_comparison(k)=0;
end;
end

Answers (3)

Walter Roberson
Walter Roberson on 22 Nov 2011
You are doing a floating point equality comparison??
I suggest that when the signals differ, you output the two values and the difference between the values. Large differences would call for different explanations than differences in the last bit.
  1 Comment
Peter
Peter on 22 Nov 2011
Actually originaly I did the same with fixed-point (16 bit) vectors. I could not find the mistake so I checked it with floating-point and the difeferences in the vectors appear at the same places as the fixed-point. Looking at the vectors I can see that in the samples where the vectors differ the 'To Workspace' element sampled the same input sample two times (it repeats the sample) instead of sampling the next sample. So in this case is acts like sample-and-hold for some of the input samples. I do not know how to avoid this, and I do not know how it is possible that for one frequency it is ok but for the other it is not. Maybe my settings are wrong but I do not know what to change.

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
You need to check the simulation step size of your Simulink model. The "From Workspace" block specifies a signal with time-value pairs. The data got written to the "To Workspace" block is determined by the simulation step. If you set the simulation solver to be fixed discrete and the fixed sample time coincide with the sample time of the "From Workspace" data, you might have a change match the input and output. Still, you need to worry about the rounding error of floating point data.
The "From Workspace" block is like specifying the room temperature at every minute. If you run your simulation at 1 second step size, it is going to interpolate the temperature at every second. If the step size is 10 minutes, it is going to down-sample it. Check the length of the "To Workspace" variable to find it out.
Not sure what is the purpose of your experiment. But you can use the "MATLAB Fcn" block to generate the signal directly in Simulink, with all the ampl, sampling_freq parameters.

shohan nur
shohan nur on 1 Aug 2018
sir, can I generate any EEG signal block which I get from (.txt) file? I want to design a filter for which filtered EEG signal that's why I need to generate a signal block in Simulink as an input of my circuit. Thank you!

Community Treasure Hunt

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

Start Hunting!