?? demoft.m
字號:
%343.m
function demoft(varargin)
%DEMODF_FFT FFT display of an incoming analong input signal.
switch nargin
case 0
data = localInitAI;%Creat the annalog input.
data = localInitFig(data);%creat the figure.
hFig = data.handle.figure;
case 1
data = [];%initiallize variables.
action = varargin{1};
hFig = varargin{2};
try
data = get(hFig,'Userdata');
end
%Based on the action ,call the apropriate local function
%create the figure.
data = localInitFig(data);
hFig = data.handle.figure;
end
%upfate the analog input object's userdata
if isvalid(data.ai)
set(data.ai,'Userdata',data);
end
%creat the object and get the first fft.
function [data,errflag] = localInitAI(varargin)
%initialize variables.
errflag = 0;
data = [];
%either no input argumentd or all three -ADAPTORNAME,ID AND
% CHANNELID.
switch nargin
case 0
adaptor = 'winsound';
id = 0;
chan = 1;
otherwise
laseterr('the ADAPTORNAME,ID and CHANID must be specified.');
errflag = 1;
return;
end
% object configration.
% create an analog input object with one channel.
ai = analoginput(adaptor,id);
addchannel(ai,chan);
%configure the anaog input object.
set(ai,'SampleRate',44100);
%configure the callback to updat the display.
set(ai,'TimerAction',{'demoai_fft','fftshowdata'});
set (ai,'TimerPeriod',0.1);
%configure the analog input objest to trigger manually twise.
set (ai,'SamplesPerTrigger',1024);
set (ai,'TriggerRepeat',1);
set (ai,'TriggerType','manual');
%object execution.
%start the analog input object .
start (ai);
trigger(ai);
%obtain the available time and data.
[d,time] = getdata(ai,ai.SamplesPerTrigger);
%calcualte the fft
Fs = get(ai,'SampleRate');
blockSize = get(ai,'SamplesPerTrigger');
[f,mag] = localDaqfft(d,Fs,blockSize);
%create the data structure.
data.ai = ai;
data.getdata = [d time];
data.daqfft = [f mag];
data.handle = [];
%set the object's userdata to data
set (data.ai,'Userdata',data);
%creat the display
function data =localInitFig(data)
%initiallize variables.
btnColor = get(0,'DefaultUIControlBackgroundColor');
%position the gui in the middle of the screen
screenUnits=get(0,'Units');
set(0,'Units','pixels');
screenSize=get(0,'ScreenSize');
set(0,'Units',screenUnits);
figWidth=600;
figHeight=360;
figPos=[(screenSize(3)-figWidth)/2 (screenSize(4)-figHeight)/2 figWidth figHeight];
%creat the figure window
hFig=figure(...
'Color' , btnColor , ...
'IntegerHandle' , 'off' ,...
'DoubleBuffer' , 'on' ,...
'DeleteFcn' , 'demoai_fft(''close'',gcbf)' ,...
'MenuBar' , 'none' ,...
'HandleVisibility' , 'on' ,...
'Name' , '模擬信號的頻譜監視' ,...
'Tag' , '模擬信號的頻譜監視' ,...
'NumberTitle' , 'off' ,...
'Units' ,'pixels' ,...
'Position' ,figPos ,...
'UserData' ,[] ,...
'Colormap' ,[] ,...
'Pointer' ,'arrow' ,...
'Visible' ,'on');
% Create Data subplot.
hAxes(1) = axes ( ...
'Position' ,[0.1300 0.5811 0.7750 0.3439],...
'Parent' , hFig ,...
'XLim' , [0 get(data.ai, 'SamplesPerTrigger')],...
'YLim' ,[-0.5 0.5]);
%Plot the data.
hLine(1) = plot(data.getdata(:,1));
set(hAxes(1),'XLim',[0 get(data.ai, 'SamplesPerTrigger')]);
%Label the plot.
xlabel('采樣');
ylabel('輸入信號幅度(v)');
title('輸入模擬信號');
%Create the FFT subplot.
hAxes(2) = axes(...
'Position' , [0.1300 0.1100 0.7750 0.3439],...
'Parent' ,hFig,...
'XLim' , [0 max(data.daqfft(:,1))]...
);
% Plot the data.
hLine(2) = plot(data.daqfft(:,1),data.daqfft(:,2));
set(hAxes(2), 'XLim' , [0 max(data.daqfft(:,1))]);
%Label the plot.
xlabel('頻率(Hz)');
ylabel('幅度(dB)');
title('監測到的頻譜');
%Create a start/stop pushbutton.
htoggle = uicontrol(...
'Parent' , hFig,...
'Style' , 'pushbutton',...
'Units' , 'normalized',...
'Position' , [0.0150 0.0111 0.1 0.0556],...
'Value' , 1,...
'String' , 'Stop',...
'Callback' , 'demoai_fft(''stop'',gcbf);');
hmenu(1) = uimenu('Parent',hFig,'Label','File');
hmenu(2)= uimenu(hmenu(1),'Label','Close demoai_fft',...
'Callback', 'demoai_fft(''close'',gcbf)');
hmenu(3) = uimenu ('Parent', hFig,'Label','Help');
hmenu(4) = uimenu (hmenu(3),'Label','Data Acquisition Toolbox',...
'Callback','helpwin(''daq'')');
hmenu(5)=uimenu(hmenu(3),'Label', 'demoai_fft',...
'Callback','helpwin(''demoai_fft'')');
%store the handles in the data matrix
data.handle.figure=hFig;
data.handle.axes=hAxes;
data.handle.line=hLine;
data.handle.toggle=htoggle;
data.state=0;
% set the axes handlevixibility to off.
set(hAxes,'HandleVisibility','off');
%store the data matrix and display figure.
set(hFig,'Visible','on','UserData',data,'HandleVisibility','off');
%close the figure window.
function localClose(data)
% stop the device if it is running and delete the object.
if isvalid(data.ai)
if strcmp(get(data.ai,'Running'),'on')
stop(data.ai);
end
delete(data.ai);
end
% close the figure window.
delete(data.handle.figure);
% stop or start the device.
function data =localStop(data)
%based on the state either stop or start.
if data.state ==0
stop(data.ai); %stop the device.
set(data.handle.toggle,'string','start');
data.state =1; % store the new state.
else
start(date.ai); % store the device.
set(data.handle.toggle,'string','stop');
data.state =0; % store the new state.
end
%計算數據的FFT
function [f,mag] = localDaqfft(data,Fs,blockSize)
% calculate the fft of the data.
xFFT = fft(data);
xfft = abs(xFFT);
% Avoid taking the log of 0.
index =find(xfft == 0);
xfft(index) = 1e-17;
mag = 20*log10(xfft);
mag = mag(1:blockSize/2);
f = (0:length(mag)-1)*Fs/blockSize;
f = f(:);
function localfftShowData(obj,event)
% get the handles.
data = obj.UserData;
hFig = data.handle.figure;
hAxes = data.handle.axes;
hLine = data.handle.line;
% execute a peekdata.
x = peekdata(obj,obj.SamplesPerTrigger);
% 計算FFt
Fs = obj.SampleRate;
blockSize = obj.SamplesPerTrigger;
[f,mag] = localDaqfft(x,Fs,blockSize);
%更新曲線
set(hLine(1),'YData',x(:,1));
set(hLine(2),'XData',f(:,1),'YData',mag(:,1));
drawnow;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -