Saturday, 29 October 2011

Getting Started with Simulink: Simple Power calculations

Getting Started with Simulink: Simple Power calculations

In this small example, we will calculate the instantaneous power along with average rms power and voltage using simulink when equation of V and I are given.

Problem:
if v(t)=10cos(120πt+30) and i(t)=6cos(120πt+60).Determine the average power and rms value of v(t).

Formulas Used:
As we know instantaneous power is given as
\[P(t)=v(t) i(t) \]
and average power is given as
\[P_{avg}=\frac{1}{T} \int_0^T{P(t)dt} \]
similarly rms voltage is given as
\[V_{rms}=\sqrt {\frac{1}{T} \int_0^T{V^2(t)dt}} \]
where T is an integer multiple of timeperiod.

Source Creation:
First we will create two sin sources V and I and adjust their parameter to match the problem
V Source:
 Amplitude: 10
  freq:          60
  phase:       pi/6
 sample time: 0.01

I Source:
 Amplitude:  6
  freq:          60
  phase:       pi/3
 sample time: 0.01

RMS Voltage
we will simple follow the equation of Vrms for the calculation.
1. first put a math function block, double click on it and change the equation as u^2
    connect this block's input from the output of V source.
    Output of this block represent V^2
 2. Now put a integrator block and connect the input of it from V^2. The output represents \[ \int_0^T{V^2(t)dt} \]
 3. Now put a divider block and connect the output of integrator to its input.
     Output of this block represents                    \[\frac{1}{T} \int_0^T{V^2(t)dt} \]
 4. Put a math function block, double click on it and change the equation as sqrt. Now connect this to
     the output of step3.
 5. Put a display block and connect the step4 output to it.

Fig1: Simulink model for RMS Voltage calculation
Instantaneous Power
Calculation of Inst, Power is very simple. Just multiply the V and I source and put a scope block to display it.
Fig2: Inst. Power
 Average Power
 Again Average Power calculation is simple and follows from RMS Voltage calculation. Put an integrator and divide block after the instant. power and connect it with the display block.
.

Wednesday, 19 October 2011

Multipages GUI forms: combining from muliple GUI m files : Links approach

We have learned to create a multipage form single GUI. This is not a very clean approach, because all the panels are on same position and it is sometime very hard to edit one panel later on. This example shows you to create different GUI for different  pages/slides and create a master gui to control them.

(See the previous Note on Multipage GUIs, at http://www.facebook.com/notes/matlab-by-examples-book/gui-working-on-multislides-form/155932614460266)

Handles
 Remember:
  Each GUI has a structure called handles. To see or edit this  structure , you can call guidata.
GET HANDLES
h=guidata(gui_reference);
SET HANDLES
guidata(gui_reference,h);
gui_reference is a double number which works as a pointer to the gui. To store this pointer as a variable, you need to call gui with an output
gui_reference=gui1;
Note that gui's handles.output also contain this pointer. You can use this output when you are writing code inside that gui.

Creating Slide Pages
 First of All create two guis both (GUI1.m and GUI2.m) with two edit box edit1,edit2 and two pushbutton pushbutton1 with title Next and second  pushbutton pushbutton2 with title Previous.

Now for navigation we will add too fields to each gui, next and prev.
So first in OpeningFcn add this two lines both in gui1 and gui2
function gui1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to a1 (see VARARGIN)


% Choose default command line output for a1
handles.output = hObject;
handles.next = 1 ;
handles.prev = 1 ;


% Update handles structure
guidata(hObject, handles);

Now go to pushbutton1_callback and add these line for displaying the next slide and disappearing itself
set(handles.next,'Visible','On');
set(handles.output,'Visible','Off');
for pushbutton2_callback do this to display previous slide.
set(handles.prev,'Visible','On');
set(handles.output,'Visible','Off');
Creating Master Slide
Now create a blank GUI with a pushbutton titled "Start"  and save it as gui0.fig. This will automatically create a gui0.m file.

Now in this file, go to guiOpeningFcn
and you will find the followin code there
function gui0_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gui0 (see VARARGIN)
% Choose default command line output for gui0
handles.output = hObject;



% Update handles structure
guidata(hObject, handles);


Now edit this code to call two slides and save their references to it.
% Choose default command line output for a1
handles.output = hObject;


handles.s1= gui1;
handles.s2= gui2;


h1=guidata(handles.s1);
h1.next = handles.s2;
h1.prev = hObject;
guidata(handles.s1,h1);


h2=guidata(handles.s2);
h2.prev = handles.s1;
h2.next = hObject;
guidata(handles.s2,h2);

% Update handles structure
guidata(hObject, handles);
handles.output
%set(handles.output,'Visible','off');
set(handles.s1,'Visible','off');
set(handles.s2,'Visible','off');guidata(hObject, handles);
As you can see, I have called gui1 and gui2 and saved there reference or pointers to handles.s1 and handles.s2. Now for navigation, we added the s2 to the next of s1(slide 1) and mastergui as previous of s1. Similarly for s2.

Now we need to code the start button to hide the master page and start the silde1.
Here it is .. simple enough now..

set(handles.output,'Visible','off');
set(handles.s1,'Visible','on');
set(handles.s2,'Visible','off');
remember that handles.output indicate the pointer to gui itself.


Final Touch: Processing your data
Now once you are done , you return to master page again because next slide of the s2 is masterpage.
Here you need to collect all the data and do whatever you want. As an example I will add all the 4 number together which were written in 4 editboxes. For easeness, Put one more button titled "Submit" and one Editbox titled Result on masterpage and write the calculation code in Submit_callback. Needless to say that you need to press this submit button once you are back on masterpage to do the final editing.

Here is the calculation code for your reference
h1=guidata(handles.s1);
a1=get(h1.'edit1','String');
a2=get(h1.'edit2','String');
h2=guidata(handles.s2);
a3=get(h2.'edit1','String');
a4=get(h2.'edit2','String');
a=str2num(a1)+str2num(a2)+str2num(a3)+str2num(a4);
set(handles.result,'String',a);
Here you are done. The benefit of the master page is that you can rearrange your slides very easily and each gui is independent.

Generation of Surface by Radar Data: 1 D Example

This Example shows how you can generate a surface from radar data in 1D plane.

Setup
In the setup, we take a 1D example where we rotate the radar direction in a plane from theta=0 to 180 and record the time t taken by signal to come back. Lets assume the velocity is v, then the distance r is
 \[r=v \times t/2\]

So in the setup if we take steps of deltatheta in rotation , we have thetamat matrix denoting different rotation
position and correspondingly we have tmat matrix from which calculate the rmat matrix.
Fig 1 Surface generation from Radar

Now calculating the surface(line here) is pretty easy.
x=rcos(theta)
y=-rsin(theta)
assuming radar is at (0,0)

Generation of Radar Data
Since we are not doing experiment here we need to generate radar data by simulation. we will generate random data using rand function.
thetamat=0:0.01:2*pi;
%generation of Radar Data(use the real recorded data here
tmat=rand(size(thetamat));
%for more smooth surface remove the comments below
%windowSize = 5;
%tmat=filter(ones(1,windowSize)/windowSize,1,1+tmat*0.3);
Generation of Surface
Here is the code:
v=1000;
rmat=v*tmat/2;

xmat=rmat.*cos(thetamat);
ymat=-rmat*.sin(thetamat);

plot(xmat,ymat);