In: Electrical Engineering
sir I want to make discrete time Fourier series on GUI MATLAB can any one tell me step by step procedure or coding for Discrete time Fourier series on GUI? pleaseeeee
you will need to save and execute these function files first, as these will be called in the main program which i will give you in the end.
%%%%%%FIRST%%%%%%%%%%%%
function mo = GUIobjects(o)
% GUIOBJECTS Gui objects class constructor. The object idenfifies and
% also monitors the state of GUI objects.
%
% See also @GUIOBJECTS/... GET, SET, SAVESTATE, SETSTATE, SETOBJECT, and
% RECOBJECT
switch nargin
case 0
% defualt figure properties
mo.fig = figure;
case 1
% inherit properties from parent object: movietool
mo.fig = o.fig;
end
if or( nargin == 0 , and( nargin == 1 , ~isa(o,'guiobjects') ) )
% Initialize Object Structure
mo.name = findobj(mo.fig,{'style'},{'pushbutton';'togglebutton';'radiobutton'; ...
'checkbox';'edit';'slider';'listbox';'popupmenu'});
mo.style = get(mo.name,'style');
mo.color = get(mo.name,'back');
mo.enable = get(mo.name,'enable');
mo.callbk = get(mo.name,'callback');
mo.state = [];
mo.paramI = [];
mo.param = [];
mo.windowI = [];
mo.window = [];
mo = class(mo,'guiobjects'); % create class of name 'GUIobjects'
% from structure mo
elseif isa(o,'guiobjects')
mo = o;
end
%%%%%%%%%%%%%second%%%%%%%
function highlight(o,hobj,status)
% @GUIOBJECTS/HIGHLIGHT Highlights log file simulated GUI object during
% movie playback.
%
% HIGHLIGHT(O,HOBJ,STATUS), where O is the guiobject object, HOBJ is
% the GUI object handle to be highlighted, and SATUS indicates whether
% to highlight 'ON' or un-highlight 'OFF'.
%
% HIGHLIGHT(O,'ALL') highlights all GUI object.
% See also @GUIOBJECTS/... GET, SET
if nargin == 2;
status = 'on';
end
if and(ischar(hobj) , strcmp(hobj,'all'))
hobj = o.name;
color = o.color;
background = eval('{''back''}');
else
index = find(hobj == o.name);
color = o.color{index};
if length(index) > 1
background = eval('{''back''}');
else
background = 'back';
end
end
switch status
case 'on'
set(hobj,background,[0.9 0.9 0]);
case 'off'
set(hobj,background,color);
end
%%%%%%%%%%%%%%%3%%%%%%%%%%%%%%%%
function [o,var1,write_flag] = recordobject(o,obj,var1)
% @GUIOBJECTS/RECORDOBJECT Identifies GUI object and obtains object properties
% for recording. A write_flag is set if a valid object with properites has
% been found.
%
% o: GUIobjects object
% obj: GUI object handle
%
% See also @GUIOBJECTS/... GET, SET
write_flag = 0;
if ishandle(obj)
object_index = find(o.name == obj);
if any(object_index)
style = o.style{object_index};
switch style
case {'togglebutton','radiobutton','checkbox','slider','listbox','popupmenu'}
new_var = get(obj,'value');
case 'pushbutton'
new_var = [];
case 'edit'
new_var = get(obj,'string');
end
old_var = o.param{object_index};
var1 = {old_var ; new_var};
o.param{object_index} = new_var;
write_flag = 1;
% Update object states
o = savestate(o);
end
end
%%%%%%%%%4%%%%%%%%%%%%%%%%%%%
function o = savestate(o,init_flag)
% @GUIOBJECTS/SAVESTATE Save GUI objects' user input properties. INIT_FLAG
% determines whether initial or current properties are saved.
%
% See also @GUIOBJECTS/... SETSTATE
if nargin == 1
init_flag = 0;
end
hobj_param = [];
for v = 1:length(o.name)
style = o.style{v};
switch style
case {'togglebutton','radiobutton','checkbox','slider','listbox','popupmenu'}
hobj_param{v,1} = get(o.name(v),'value');
case 'pushbutton'
hobj_param{v,1} = [];
case {'edit'}
hobj_param{v,1} = get(o.name(v),'string');
end
end
if init_flag
o.paramI = hobj_param;
o.windowI = get(o.fig,{'ButtonDownFcn','Pointer','WindowButtonDownFcn', ...
'WindowButtonMotionFcn','WindowButtonUpFcn'});
o.window = o.windowI;
else
o.param = hobj_param;
o.window = get(o.fig,{'ButtonDownFcn','Pointer','WindowButtonDownFcn', ...
'WindowButtonMotionFcn','WindowButtonUpFcn'});
end
%%%%%%%%%%5th%%%%%%%%%%%%%%%%
function set(o,varargin)
% @GUIOBJECTS/SET Set GUIobjects properties to the specified values
% and return the updated object. The following are valid properties:
% figure, enable.
%
% o = set(o,'figure',VALUE), where VALUE is 1-sets initial window
% properties, 2-sets null window properties, and if no VALUE has been
% specified, then current window peoperties are assigned.
property_argin = varargin;
while length(property_argin) >= 2,
prop = property_argin{1};
val = property_argin{2};
property_argin = property_argin(3:end);
switch prop
case 'figure'
switch val
case 'initial'
window = o.windowI;
case 'current'
window = o.window;
case 'arrow'
window = {'','arrow','','',''};
end
set(o.fig,{'ButtonDownFcn','Pointer','WindowButtonDownFcn', ...
'WindowButtonMotionFcn','WindowButtonUpFcn'},window);
case 'enable'
switch val
case 'on'
set(o.name,{'enable'},o.enable);
case 'off'
set(o.name,'enable','off');
end
otherwise
error('GUIobjects properties: figure, enable')
end
end
%%%%%%%%%%%%%%6%%%%%%%%%%%%%%%%
function setobject(o,callback,playdata)
% @GUIOBJECTS/SETOBJECT Identifies GUI object and sets object property
% according to file data entry.
%
% o: GUIobjects object
% callback: GUI object callback
% playdata: movie frame data
%
Lcbk = length(o.callbk);
current_callbk(1:Lcbk) = 0;
for i = 1:Lcbk
if ~isempty( findstr(o.callbk{i},callback) )
current_callbk(i) = i;
else
current_callbk(i) = 0;
end
end
object_index = max(current_callbk);
if any(object_index)
object = o.name(object_index);
style = o.style{object_index};
set(o.fig,'CurrentObject',object);
if ~isempty(playdata)
if strcmp(playdata{3},'next')
index = 2; else index = 1;
end
if isempty(playdata{1})
var = [];
elseif ~strcmp(style,'pushbutton')
if length(playdata{1}) == 1
var = playdata{1};
else
var = playdata{1}{index};
end
switch style
case {'togglebutton','radiobutton','checkbox','slider','listbox','popupmenu'}
set(object,'value',var);
case {'pushbutton'}
case 'edit'
set(object,'string',var);
end
end
end
highlight(o,object);
end
%%%%7%%%%%%%%%%%%%%%%%%
function setstate(o,init_flag)
% @GUIOBJECTS/SAVESTATE Set GUI objects' to data from the log file. INIT_FLAG
% determines whether initial or current properties are assigned.
%
% mt: movietool object
% method: state operation: 'initialize'|'save_state'|'set_state'
% varargin: 1 = 'initialize'
%
if nargin == 1
init_flag = 0;
end
% Set WINDOW Parameters
if init_flag
param = o.paramI;
set(o,'figure','initial');
else
param = o.param;
set(o,'figure','current');
end
% Set OBJECT Parameters
for v = 1:length(o.name)
style = o.style{v};
set(o.fig,'CurrentObject',o.name(v));
switch style
case {'radiobutton','checkbox','slider','listbox','popupmenu'}
param_current = get(o.name(v),'value');
if param_current ~= param{v}
set(o.fig,'CurrentObject',o.name(v))
set(o.name(v),'value',param{v});
eval(o.callbk{v});
end
case 'togglebutton'
param_current = get(o.name(v),'value');
if param_current ~= param{v}
set(o.fig,'CurrentObject',o.name(v))
set(o.name(v),'value',param{v});
eval(o.callbk{v});
end
case 'pushbutton'
%set(o.name(v),'value',param{v});
case 'edit'
param_current = get(o.name(v),'string');
if ~strcmp(param_current,param{v})
set(o.fig,'CurrentObject',o.name(v))
set(o.name(v),'string',param{v});
eval(o.callbk{v});
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%
function fouriergui_callbacks(action)
% This has the callback routines for Fourier Series Demo.
NO = 0; YES = 1; OFF = 0; ON = 1;
%-------------------------------------------------------------------------------
% Default Settings
%-------------------------------------------------------------------------------
if nargin == 0
action = 'Initialize';
else
h = getappdata(gcbf, 'Handles');
mt = getappdata(gcbf,'movietoolData');
if ~isempty(mt), pre_callbackAction(mt,action,gcbo,[],h); end
end
switch(action)
%---------------------------------------------------------------------------
case 'Initialize'
%---------------------------------------------------------------------------
try
% All error checking moved to the DCONVDEMO function. Keep this here as
% well because we need the Matlab version number for some of the bug
% workarounds.
h.MATLABVER = versioncheck(5.2); % Check Matlab Version
%--- Set up GUI ---%
fouriergui;
strVersion = '1.44'; % Version string for figure title v1.3 (JMc 25-Nov-2011)
set(gcf, 'Name', ['Fourier Series Demo v' strVersion]);
h.LineWidth = 0.5;
h.FigPos = get(gcf,'Pos');
SCALE = getfontscale; % Platform dependent code to determine
% SCALE parameter
setfonts(gcf,SCALE); % Setup fonts: override default fonts used
% in fouriergui
configresize(gcf); % Change all 'units'/'font units' to normalized
h = gethandles(h); % Get GUI graphic handles
h = defaultplots(h); % Create default plots
if h.MATLABVER >= 6.0
%--User-Data Acquisition (movietool object)--%
movietool('Initialize',gcf,mfilename,0.08);
%-------------------------%
end
setappdata(gcf,'Handles',h);
% correct fontsize (why ??)
font_size = get(h.Axis.Magnitude,'fontsize');
set(h.Axis.Phase,'fontsize',font_size);
set(gcf,'WindowButtonMotionFcn',[mfilename ' WindowButtonMotionFcn']);
set(gcf,'HandleVisibility','callback'); % Make figure inaccessible
% from command line
catch
%--- Delete any GUI figures and remove from path if necessary --%
delete(findall(0,'type','figure','tag','fseriesdemo'));
%--- Display the error to the user and exit ---%
errordlg(lasterr,'Error Initializing Figure');
return;
end
%---------------------------------------------------------------------------
case 'SetFigureSize'
%---------------------------------------------------------------------------
OldUnits = get(0, 'Units');
set(0, 'Units','pixels');
ScreenSize = get(0,'ScreenSize');
set(0, 'Units', OldUnits);
FigSize = [0.1*ScreenSize(3), 0.15*ScreenSize(4), 0.8*ScreenSize(3), 0.7*ScreenSize(4)];
set(gcbf, 'Position', FigSize);
%--------------------------------------------------------------------------
case 'Help'
%--------------------------------------------------------------------------
hBar = waitbar(0.25,'Opening internet browser...');
DefPath = which(mfilename);
DefPath = ['file:///' strrep(DefPath,filesep,'/') ];
URL = [ DefPath(1:end-22) , 'help/','index.html'];
if h.MATLABVER >= 6
STAT = web(URL,'-browser');
else
STAT = web(URL);
end
waitbar(1);
close(hBar);
switch STAT
case {1,2}
s = {'Either your internet browser could not be launched or' , ...
'it was unable to load the help page. Please use your' , ...
'browser to read the file:' , ...
' ', ' index.html', ' ', ...
'which is located in the Fourier help directory.'};
errordlg(s,'Error launching browser.');
end
%--------------------------------------------------------------------------
case 'Screenshot'
%--------------------------------------------------------------------------
screenshotdlg(gcbf);
%--------------------------------------------------------------------------
case 'ShowMenu'
%--------------------------------------------------------------------------
check = get(gcbo,'Checked');
if strcmp(check,'off')
set(gcbf,'MenuBar','figure');
set(gcbo,'Checked','On');
else
set(gcbf,'MenuBar','none');
set(gcbo,'Checked','Off');
end
%--------------------------------------------------------------------------
case 'Close'
%--------------------------------------------------------------------------
close(gcbf);
%--------------------------------------------------------------------------
case 'LineWidth'
%--------------------------------------------------------------------------
h.LineWidth = linewidthdlg(h.LineWidth);
set(findobj(gcbf, 'Type', 'line'), 'LineWidth', h.LineWidth);
setappdata(gcbf,'Handles',h);
%--------------------------------------------------------------------------
case {'SignalType','SignalType1'}
%--------------------------------------------------------------------------
popValue = get(h.PopUpMenu.Signal, 'Value');
if popValue == 1
% Square wave
h.yval = sqar(2*pi*h.freq.*h.timeaxis);
elseif popValue == 2
% Triangle wave
timeaxis = h.timeaxis(h.timeaxis >= 0);
yval = tri(10/h.freq,1,length(timeaxis));
h.yval = -[fliplr(yval(2:end)) yval];
elseif popValue == 3
% Ramp wave
T = 1/h.freq;
timeaxis = h.timeaxis(h.timeaxis >= 0);
yval = (mod(timeaxis+T/2,T)-T/2)/T*2;
h.yval = [-fliplr(yval(2:end)) yval];
elseif popValue == 4
% Full-wave Rectified Sine wave
h.yval = fullwave(h.timeaxis,1/h.freq,'sine');
elseif popValue == 5
% Full-wave Rectified Cosine wave
h.yval = fullwave(h.timeaxis,1/h.freq, 'cosine');
elseif popValue == 6
% Half-wave Rectified Sine wave
h.yval = halfwave(h.timeaxis,1/h.freq, 'sine');
elseif popValue == 7
% Full-wave Rectified Cosine wave
h.yval = halfwave(h.timeaxis,1/h.freq, 'cosine');
end
changeplots(h);
setappdata(gcbf,'Handles',h);
%--------------------------------------------------------------------------
case {'FourCoeff1','FourCoeff2'}
%--------------------------------------------------------------------------
Tag = get(gcbo, 'Tag');
MAKECHANGE = YES;
if strcmp(Tag, 'FourCoeffSlider')
h.Slider.CoeffValue = round(get(gcbo, 'Value'));
else
NewCoeffValue = str2num(get(gcbo, 'String'));
if ( NewCoeffValue < get(h.Slider.NumCoeff,'Min') ) ...
| (NewCoeffValue > get(h.Slider.NumCoeff,'Max'))
set(gcbo,'String',num2str(h.Slider.CoeffValue));
MAKECHANGE = NO;
else
h.Slider.CoeffValue = round(NewCoeffValue);
end
end
if MAKECHANGE
set(h.Edit.NumCoeff, 'String', num2str(h.Slider.CoeffValue));
set(h.Slider.NumCoeff, 'Value', h.Slider.CoeffValue);
if get(h.Slider.NumCoeff, 'Value')
set(h.Text.SliderText, 'String', sprintf('Coefficients from k = -%d to k = %d',h.Slider.CoeffValue,h.Slider.CoeffValue ));
else
set(h.Text.SliderText, 'String','DC Coefficient: k = 0');
end
setappdata(gcbf,'Handles',h);
end
changeplots(h);
%--------------------------------------------------------------------------
case {'changex','changex1'}
%--------------------------------------------------------------------------
xlabel = -15:5:15;
if get(findobj('tag','changex'),'value')
set(findobj('tag','MagnitudeAxis'),'xtick',xlabel,'xticklabel',round(1000.*h.freq*xlabel)/1000);
set(findobj('tag','PhaseAxis'),'xtick',xlabel,'xticklabel',round(1000.*h.freq*xlabel)/1000);
set([get(h.Axis.Magnitude,'XLabel'),get(h.Axis.Phase,'XLabel')],'String','Frequency (Hz)');
else
set(findobj('tag','MagnitudeAxis'),'xtick',xlabel,'xticklabel',xlabel);
set(findobj('tag','PhaseAxis'),'xtick',xlabel,'xticklabel',xlabel);
set([get(h.Axis.Magnitude,'XLabel'),get(h.Axis.Phase,'XLabel')],'String','Number of Fourier Coefficients');
end
%--------------------------------------------------------------------------
case 'ShowError'
%--------------------------------------------------------------------------
if get(findobj('tag','showerror'),'value')
set([h.Signal.Waveform,h.Signal.RecWaveform,h.DCtext],'vis','off');
set(h.Signal.ErrorWaveform,'vis','on');
set([findobj('tag','origsig'),findobj('tag','syntsig')],'vis','off');
set(findobj('tag','errosig'),'vis','on');
else
set([h.Signal.Waveform,h.Signal.RecWaveform],'vis','on');
set(h.Signal.ErrorWaveform,'vis','off');
coeffValue = get(h.Slider.NumCoeff, 'Value');
if coeffValue == 0
set(h.DCtext,'vis','on');
end
set([findobj('tag','origsig'),findobj('tag','syntsig')],'vis','on');
set(findobj('tag','errosig'),'vis','off');
changeplots(h);
end
%--------------------------------------------------------------------------
case 'ChangePeriod'
%--------------------------------------------------------------------------
T0 = get(gcbo,'value');
set(findobj('tag','TextforPeriodSignal'),'string',['Choose the Signal Period: T = ' num2str(T0)]);
h.timeaxis = -30:0.1:30;
h.freq = 1/T0;
setappdata(gcbf,'Handles',h);
fouriergui_callbacks changex1
fouriergui_callbacks SignalType1
%--------------------------------------------------------------------------
case 'WindowButtonMotionFcn'
%--------------------------------------------------------------------------
[mouse_x,mouse_y,fig_size] = mousepos;
old_unitsMag = get(h.Axis.Magnitude,'units');
set([h.Axis.Magnitude,h.Axis.Phase],'units','pixels');
axMag = get(h.Axis.Magnitude,'pos');
axPha = get(h.Axis.Phase,'pos');
set([h.Axis.Magnitude,h.Axis.Phase],'units',old_unitsMag);
% Cursor over object axes flag
over_axesMag_flg = any( (mouse_x > axMag(1)) & (mouse_x < axMag(1)+axMag(3)) & ...
(mouse_y > axMag(2)) & (mouse_y < axMag(2)+axMag(4)) );
over_axesPha_flg = any( (mouse_x > axPha(1)) & (mouse_x < axPha(1)+axPha(3)) & ...
(mouse_y > axPha(2)) & (mouse_y < axPha(2)+axPha(4)) );
if over_axesMag_flg
% pointer over Magnitude axes
current_pt = get(h.Axis.Magnitude,'CurrentPoint');
xvals = get(h.Spectrum.Magnitude(1),'xdata');
yvals = get(h.Spectrum.Magnitude(1),'ydata');
xtoll = 0.45;
ytoll = 0.02;
[xindex] = find(abs(xvals-current_pt(1)) < xtoll);
if ~isempty(xindex)
if yvals(xindex) == 0 & (current_pt(3) <= ytoll)
% mag = 0
set(h.Line.Magnitude,'xdata',xvals(xindex),'ydata',0,'vis','on');
set(h.Text.Magnitude,'position',[xvals(xindex) yvals(xindex)+ytoll],'string',0,'horiz','center','vert','bottom');
elseif (current_pt(3) <= (yvals(xindex)+ytoll))
if xvals(xindex) >= 0
horiz = 'left';
else
horiz = 'right';
end
set(h.Text.Magnitude,'pos',[xvals(xindex) yvals(xindex)+ytoll],'string',num2str(round(1000*yvals(xindex))/1000), ...
'horiz',horiz,'vert','bottom');
set(h.Line.Magnitude(1),'xdata', xvals(xindex),'ydata',yvals(xindex),'vis','on');
set(h.Line.Magnitude(2),'xdata', [xvals(xindex) xvals(xindex)],'ydata',[0 yvals(xindex)],'vis','on');
end
else
set(h.Text.Magnitude,'string','');
set(h.Line.Magnitude,'vis','off');
end
elseif over_axesPha_flg
% pointer over Phase axes
current_pt = get(h.Axis.Phase,'CurrentPoint');
xvals = get(h.Spectrum.Phase(1),'xdata');
yvals = get(h.Spectrum.Phase(1),'ydata');
xtoll = 0.2;
ytoll = 1.1;
ytollM = 1e-7;
[xindex] = find(abs(xvals-current_pt(1)) < xtoll);
if ~isempty(xindex)
if (yvals(xindex) == 0) & (abs(current_pt(3)) < ytoll)
set(h.Line.Phase,'xdata',[xvals(xindex) xvals(xindex)],'ydata',[0 0],'vis','on');
set(h.Text.Phase,'position',[xvals(xindex) ytoll],'string','0');
elseif (current_pt(3) <= yvals(xindex)) & (current_pt(3) >= 0)
set(h.Line.Phase(1),'xdata', xvals(xindex),'ydata',yvals(xindex),'vis','on');
set(h.Line.Phase(2),'xdata', [xvals(xindex) xvals(xindex)],'ydata',[0 yvals(xindex)],'vis','on');
if abs(yvals(xindex) - pi) < ytollM
set(h.Text.Phase,'position',[xvals(xindex) yvals(xindex)+ytoll],'string','\pi');
elseif abs(yvals(xindex) - pi/2) < ytollM
set(h.Text.Phase,'position',[xvals(xindex) yvals(xindex)+ytoll],'string','0.5\pi');
else
set(h.Text.Phase,'position',[xvals(xindex) yvals(xindex)+ytoll],'string',num2str(round(1000*yvals(xindex))/1000));
end
elseif (current_pt(3) >= yvals(xindex)) & (current_pt(3) <= 0)
set(h.Line.Phase(1),'xdata', xvals(xindex),'ydata',yvals(xindex),'vis','on');
set(h.Line.Phase(2),'xdata', [xvals(xindex) xvals(xindex)],'ydata',[0 yvals(xindex)],'vis','on');
if abs(yvals(xindex) + pi) < ytollM
set(h.Text.Phase,'position',[xvals(xindex) yvals(xindex)-ytoll],'string','-\pi');
elseif abs(yvals(xindex) + pi/2) < ytollM
set(h.Text.Phase,'position',[xvals(xindex) yvals(xindex)-ytoll],'string','-0.5\pi');
else
set(h.Text.Phase,'position',[xvals(xindex) yvals(xindex)-ytoll],'string',num2str(round(1000*yvals(xindex))/1000));
end
else
set(h.Text.Phase,'string','');
set(h.Line.Phase,'vis','off');
end
else
% pointer neither over Magnitude or Phase axes
set([h.Text.Magnitude,h.Text.Phase],'string','');
set([h.Line.Magnitude,h.Line.Phase],'vis','off');
end
end
%--------------------------------------------------------------------------
otherwise
%--------------------------------------------------------------------------
error('Illegal Action');
end
if nargin > 0 & ishandle(gcbf)
h = getappdata(gcbf);
if ~isempty(mt), post_callbackAction(mt,action,gcbo,[],h); end
end
%% endfunction fouriergui_callbacks ============================================
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
function changeplots(h)
% CHANGEPLOTS
coeffValue = get(h.Slider.NumCoeff, 'Value');
popValue = get(h.PopUpMenu.Signal, 'Value');
index_coeff = round(coeffValue);
magVec = [];
h.recFinal = zeros(1,length(h.timeaxis));
switch popValue
%--------------- Square wave ------------------------
case 1
for n = -index_coeff:index_coeff
if n == 0 | mod(n,2)==0
magSpec = 0;
else
magSpec = (-2*j)/(n*pi);
end
recSig = magSpec * exp(j*2*pi*h.freq*n*h.timeaxis);
magVec = [magVec;magSpec];
h.recFinal = h.recFinal+recSig;
if coeffValue == 0 & ~get(findobj('tag','showerror'),'value')
set(h.DCtext,'pos',[0.4,0.3],'vis','on');
else
set(h.DCtext,'vis','off');
end
set(h.Axis.Magnitude,'ylim',[0 0.78]);
end
%--------------- Triangle wave ----------------------
case 2
for n = -index_coeff:index_coeff
if n == 0 | mod(n,2)==0
magSpec = 0;
else
magSpec = 4/((n*pi)^2);
end
recSig = magSpec * exp(j*2*pi*h.freq*n*h.timeaxis);
magVec = [magVec;magSpec];
h.recFinal = h.recFinal+recSig;
if coeffValue == 0 & ~get(findobj('tag','showerror'),'value')
set(h.DCtext,'pos',[9,.3],'vis','on');
else
set(h.DCtext,'vis','off');
end
set(h.Axis.Magnitude,'ylim',[0 0.5]);
end
%--------------- Ramp/Sawtooth wave -----------------
case 3
for n = -index_coeff:index_coeff
if n == 0
magSpec = 0;
else
magSpec = ((-1)^n)*(j)/(n*pi);
end
recSig = magSpec * exp(j*2*pi*h.freq*n*h.timeaxis);
magVec = [magVec;magSpec];
h.recFinal = h.recFinal+recSig;
if coeffValue == 0 & ~get(findobj('tag','showerror'),'value')
set(h.DCtext,'pos',[-2,0.3],'vis','on');
else
set(h.DCtext,'vis','off');
end
set(h.Axis.Magnitude,'ylim',[0 0.4]);
end
%------------------- Full-wave Rectified (Sine / Cosine) ----------------------
case {4, 5}
for n = -index_coeff:index_coeff
% We dont need to have special case
% Sine wave coeff
magSpec = -(exp(-j*2*pi*n) + 1)/(pi*(4*n^2 -1));
if popValue == 5
% Cosine wave
%magSpec = exp(-j*n*pi)*magSpec;
magSpec = 2*cos(n*pi) / (pi*(1-4*n^2));
end
recSig = (magSpec * exp(j*2*pi*h.freq*n*h.timeaxis));
magVec = [magVec;magSpec];
h.recFinal = h.recFinal+recSig;
if coeffValue == 0 & ~get(findobj('tag','showerror'),'value')
if popValue == 4
set(h.DCtext,'pos',[-1,0.95],'vis','on');
else
set(h.DCtext,'pos',[4,0.95],'vis','on');
end
else
set(h.DCtext,'vis','off');
end
set(h.Axis.Magnitude,'ylim',[0 0.77]);
end
%-------------------- Half-wave Rectified (Sine / Cosine) ---------------------
case {6,7}
for n = -index_coeff:index_coeff
% Cosine wave coeffs
if n == 0
magSpec = 1/(pi);
elseif n== -1 | n== 1
magSpec = 1/4;
else
magSpec = ( cos(n*pi/2))/pi/(1-n^2);
end
% Change coefficient depending cosine / sine
if popValue == 6
% Sine wave
magSpec = exp(-j*n*pi/2)*magSpec;
end
recSig = magSpec * exp(j*2*pi*h.freq*n*h.timeaxis);
magVec = [magVec;magSpec];
h.recFinal = h.recFinal+recSig;
if coeffValue == 0 & ~get(findobj('tag','showerror'),'value')
set(h.DCtext,'pos',[10,0.6],'vis','on');
else
set(h.DCtext,'vis','off');
end
set(h.Axis.Magnitude,'ylim',[0 0.41]);
end
otherwise
error('Invalid Signal Type');
end
h.magVec = magVec;
h.SpectrumXval = -index_coeff:index_coeff;
h.magVec(abs(h.magVec) < sqrt(eps)) = 0;
%plot & Stem
set(h.Signal.Waveform, 'XData',h.timeaxis,'YData', h.yval);
set(h.Signal.RecWaveform, 'XData',h.timeaxis,'YData', real(h.recFinal));
set(h.Signal.ErrorWaveform,'XData',h.timeaxis,'Ydata',h.yval-real(h.recFinal));
stemdata(h.SpectrumXval,abs(h.magVec),[],h.Spectrum.Magnitude);
stemdata(h.SpectrumXval,angle(h.magVec),[],h.Spectrum.Phase);
setappdata(gcbf,'Handles',h);
% endfunction changeplots
% eof: fouriergui_callbacks.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function fseriesdemo()
% FSERIESDEMO is a Graphical User Interface which illustrates
% fourier series.
%--------------------------------------------------------------------
% Startup error checks
%--------------------------------------------------------------------
%
% : Check the Matlab version
% : Check the screen size
% : Check the path
%--------------------------------------------------------------------
errCmd = 'errordlg(lasterr,''Error Initializing Figure''); error(lasterr);';
cmdCheck1 = 'installcheck;';
cmdCheck2 = 'h.MATLABVER = versioncheck(5.2);';
cmdCheck3 = 'screensizecheck([800 600]);';
cmdCheck4 = ['adjustpath(''' mfilename ''');'];
eval(cmdCheck1,errCmd); % Simple installation check
eval(cmdCheck2,errCmd); % Check Matlab Version
eval(cmdCheck3,errCmd); % Check Screen Size
eval(cmdCheck4,errCmd); % Adjust path if necessary
%--------------------------------------------------------------------
% Run the GUI
%--------------------------------------------------------------------
fouriergui_callbacks;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The code will run or not is subjected to the version of matlab you are using.
Save all the functions seperately with their original name.