No BSD License  

Highlights from
Spectrum Analysis

image thumbnail

Spectrum Analysis

by

 

Spectrum analysis in a waterfall display

TDSSpecDemo(varargin)
function varargout = TDSSpecDemo(varargin)
% TDSSpecDemo Application M-file for TDSSpecDemo.fig
%    FIG = TDSSpecDemo launch TDSSpecDemo GUI.
%    TDSSpecDemo('callback_name', ...) invoke the named callback.

% Last Modified by GUIDE v2.0 24-Aug-2001 09:25:54

if nargin == 0  % LAUNCH GUI
  
  fig = openfig(mfilename,'reuse');

  % Use system color scheme for figure:
  set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));

  % Generate a structure of handles to pass to callbacks, and store it. 
  handles = guihandles(fig);
  guidata(fig, handles);

  if nargout > 0
    varargout{1} = fig;
  end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

  try
    [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
  catch
    disp(lasterr);
  end

end


% --------------------------------------------------------------------
function varargout = StartButton_Callback(h, eventdata, handles, varargin)
global instr idn;

% Remove choice of Exit
pushbuttonExitButton = findobj(gcbf,'Tag','ExitButton');
set(pushbuttonExitButton,'Enable','off');

texthandleToolBarString = findobj(gcbf,'Tag','ToolBarString');
set(texthandleToolBarString,'String',['Connected to ' idn 'Running ... Zoom In by Left Click and Drag; Zoom Out By Right Click or using App. Max Zoom Out Button.']);

% get object handles
pushbuttonStartButton = findobj(gcbf,'Tag','StartButton');
pushbuttonStopButton = findobj(gcbf,'Tag','StopButton');
pushbuttonZoomOutButton = findobj(gcbf,'Tag','ZoomOutButton');
texthandleStatusBarString = findobj(gcbf,'Tag','StatusBarString');
texthandleRecordLengthString = findobj(gcbf,'Tag','RecordLengthString');
texthandleSamplingRateString = findobj(gcbf,'Tag','SamplingRateString');
edithandleFFTWindow = findobj(get(gcbf,'Children'),'flat','Tag','FFTWindowInput');
pushbuttonSampleRateMinus = findobj(gcbf,'Tag','SampleRateMinus');
pushbuttonSampleRatePlus = findobj(gcbf,'Tag','SampleRatePlus');
pushbuttonRecordLengthMinus = findobj(gcbf,'Tag','RecordLengthMinus');
pushbuttonRecordLengthPlus = findobj(gcbf,'Tag','RecordLengthPlus');
pushbuttonConnectButton = findobj(gcbf,'Tag','ConnectButton');
pushbuttonChannelSelectionButton = findobj(gcbf,'Tag','ChannelSelection');
popupmenuInstrumentAddress = findobj(gcbf,'Tag','InstrumentAddress');
popupmenuBoardNumber = findobj(gcbf,'Tag','BoardNumber');

set(pushbuttonStopButton,'UserData',0);
set(pushbuttonExitButton,'UserData',0);
set(pushbuttonZoomOutButton,'UserData',0);

i = 0;
chNumber = get(pushbuttonChannelSelectionButton,'Value');

fprintf(instr,'HEADER OFF');
fprintf(instr,['DATA:SOURCE ' 'CH' num2str(chNumber)]);
fprintf(instr,['SELECT:' 'CH' num2str(chNumber) ' ON']);
fprintf(instr,'DATA:ENCDG SRIBINARY;WIDTH 1');
fprintf(instr,'ACQUIRE:STATE OFF');
fprintf(instr,'ACQUIRE:MODE NORMALSAMPLE');
fprintf(instr,'ACQUIRE:STOPAFTER SEQUENCE');

while((get(pushbuttonExitButton,'UserData')~=1) & (get(pushbuttonStopButton,'UserData')~=1))
      tstart = clock;
      i = i+1;
      try
          [data,fs] = tekgetcurve(instr);
      catch
          set(texthandleToolBarString,'String',{'There was an error in the MATLAB demo. Please restart MATLAB and the Instrument.'});
          set(pushbuttonExitButton,'Enable','on');
          set(pushbuttonSampleRateMinus,'Enable','off');
          set(pushbuttonSampleRatePlus,'Enable','off');
          set(pushbuttonRecordLengthMinus,'Enable','off');
          set(pushbuttonRecordLengthPlus,'Enable','off');
          set(pushbuttonStartButton,'Enable','off');
          set(pushbuttonStopButton,'Enable','off');
          set(edithandleFFTWindow,'Enable','off');
          set(pushbuttonZoomOutButton,'Enable','off');
          set(pushbuttonChannelSelectionButton,'Enable','off');
          set(popupmenuBoardNumber,'Enable','off');
          set(pushbuttonConnectButton,'Enable','off');
          set(popupmenuInstrumentAddress,'Enable','off');
          set(popupmenuBoardNumber,'Enable','off');
          break;
      end;
      set(texthandleRecordLengthString,'String',{'Rec Length' ; tekgetunits(length(data))});
      set(texthandleSamplingRateString,'String',{'Sample Rate'; [tekgetunits(fs) 'S/s']});
      
      L = length(data);
      fvect = fs*(0:(L-1))/L;	% Hz
      L2 = ceil(L/2);
      
      windowchoice = get(edithandleFFTWindow,'Value');
      if windowchoice == 2
         fftwindow = hanning(L)/0.5;
      elseif windowchoice == 3
         fftwindow = hamming(L)/0.54;
      elseif windowchoice == 4
         fftwindow = blackman(L)/0.42;
      else
         fftwindow = ones(L,1); 
      end;
      
      % compute the spectrum
	  fftdata = fft(data(1:L,1).*fftwindow);
	  % scale for getting the results in dBm
      fftdata = 20*log10(abs(fftdata))-20*log10(L)+20*log10(2)+10;
      % crop the result to positive frequencies
      fftdata = fftdata(1:L2);
      % create the correct size horizontal axis
      fvect = fvect(1:L2);
      
         if ((i == 1) | get(pushbuttonZoomOutButton,'UserData')==1) 
             set(gca,'NextPlot','replace');
             set(pushbuttonZoomOutButton,'UserData',0);
             handleLine = plot(fvect,fftdata); 
             grid on;
             zoom on;
             xlabel(['Frequency [Hz]']);
             ylabel('dBm');
             title('Spectrum');
             drawnow;
         else
             % The axes of the last plot - resulted from zoom in/out - are preserved 
             % set(gca,'NextPlot','replacechildren');
             set(handleLine,'XData',fvect,'YData',fftdata);
             drawnow;
         end;
         tstop = clock;
         set(texthandleStatusBarString,'String',['Acquired and Analyzed/FFTed ' num2str(L) ' data points in ' num2str(etime(tstop,tstart)) 's. This is run number ' num2str(i) '.']);
end;

% --------------------------------------------------------------------
function varargout = ExitButton_Callback(h, eventdata, handles, varargin)
global instr;

set(gcbo,'UserData',1);
try
    fclose(instr);
    delete(instr);
    clear instr;
catch
    instrreset;
    texthandleToolBarString = findobj(gcbf,'Tag','ToolBarString');
    set(texthandleToolBarString,'String','Could not talk to Tektronix TDS instrument. Check cables and try connecting again.');
end;
close;


% --------------------------------------------------------------------
function varargout = ConnectButton_Callback(h, eventdata, handles, varargin)
global instr idn;

visa_vendor = 'tek';
try
    fclose(instrfind);
catch
    % No instruments were found.
end;

pushbuttonExitButton = findobj(gcbf,'Tag','ExitButton');
set(pushbuttonExitButton,'Enable','off');

texthandleToolBarString = findobj(gcbf,'Tag','ToolBarString');
set(texthandleToolBarString,'String',['Connecting...']);

popupmenuInstrumentAddress = findobj(gcbf,'Tag','InstrumentAddress');
InstrumentAddress = get(popupmenuInstrumentAddress,'Value') - 1;

popupmenuBoardNumber = findobj(gcbf,'Tag','BoardNumber');
BoardNumber = get(popupmenuBoardNumber,'Value')-1;

try
    instr = visa(visa_vendor,['GPIB' num2str(BoardNumber) '::' num2str(InstrumentAddress) '::INSTR']);
    fopen(instr);
    fprintf(instr,'HEADER OFF');
    idn = query(instr, '*IDN?');
    set(texthandleToolBarString,'String',['Connected to ' idn 'Configuring...']);
    recordLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
    fclose(instr);
    % Make sure the size of the InputBuffer - in bytes - is sufficient.
    set(instr,'InputBufferSize',recordLen*1+100);
    fopen(instr);
    
    samplingInterval = str2num(query(instr,'WFMOUTPRE:XINCR?'));
    samplingRate = 1/samplingInterval;
    texthandleSamplingRateString = findobj(gcbf,'Tag','SamplingRateString');
    set(texthandleSamplingRateString,'String',{'Sample Rate'; [tekgetunits(samplingRate) 'S/s']});
    
    horizLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
    texthandleRecordLengthString = findobj(gcbf,'Tag','RecordLengthString');
    set(texthandleRecordLengthString,'String',{'Rec Length' ; tekgetunits(horizLen)});

    pushbuttonSampleRateMinus = findobj(gcbf,'Tag','SampleRateMinus');
    set(pushbuttonSampleRateMinus,'Enable','on');
    pushbuttonSampleRatePlus = findobj(gcbf,'Tag','SampleRatePlus');
    set(pushbuttonSampleRatePlus,'Enable','on');
    pushbuttonRecordLengthMinus = findobj(gcbf,'Tag','RecordLengthMinus');
    set(pushbuttonRecordLengthMinus,'Enable','on');
    pushbuttonRecordLengthPlus = findobj(gcbf,'Tag','RecordLengthPlus');
    set(pushbuttonRecordLengthPlus,'Enable','on');
    pushbuttonStartButton = findobj(gcbf,'Tag','StartButton');
    set(pushbuttonStartButton,'Enable','on');
    pushbuttonStopButton = findobj(gcbf,'Tag','StopButton');
    set(pushbuttonStopButton,'Enable','on');
    edithandleFFTWindow = findobj(gcbf,'Tag','FFTWindowInput');
    set(edithandleFFTWindow,'Enable','on');
    pushbuttonZoomOutButton = findobj(gcbf,'Tag','ZoomOutButton');
    set(pushbuttonZoomOutButton,'Enable','on');
    set(popupmenuInstrumentAddress,'Enable','off');
    set(popupmenuBoardNumber,'Enable','off');
    pushbuttonConnectButton = findobj(gcbf,'Tag','ConnectButton');
    set(pushbuttonConnectButton,'Enable','off');
    
    set(texthandleToolBarString,'String',['Connected to ' idn 'For a quick start connect the Scope Probe Comp. to CH1, press Scope AUTOSET, and then this App Start.']);
    
catch
    set(texthandleToolBarString,'String',{'Could not talk to Tektronix TDS instrurment. Check cables and try connecting again. Verify the Board Number (8 for running inside scope), Instrument GPIB Address, and Tektronix VISA and MATLAB Instrument Control Toolbox Instalation.'});
end;


% --------------------------------------------------------------------
function varargout = RecordLengthMinus_Callback(h, eventdata, handles, varargin)
global instr idn;

texthandleToolBarString = findobj(gcbf,'Tag','ToolBarString');
set(texthandleToolBarString,'String',['Attempting to Decrease Record Length. Please Wait ...']);

horizLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
newhorizLen = horizLen;
k = 0;

while ((newhorizLen == horizLen) & (k < 2))
   % If 2 consecutive attempts do not produce a new Record Length it is the min.
   k = k+1; 
   fprintf(instr,['Horizontal:RecordLength ' num2str(horizLen/(2^k))]);
   newhorizLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
end;

recordLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
fclose(instr);
% Make sure the size of the InputBuffer - in bytes - is sufficient.
set(instr,'InputBufferSize',recordLen*1+100);
fopen(instr);

texthandleRecordLengthString = findobj(gcbf,'Tag','RecordLengthString');
set(texthandleRecordLengthString,'String',{'Rec Length' ; tekgetunits(recordLen)});
set(texthandleToolBarString,'String',['Connected to ' idn 'Running ... Zoom In by Left Click and Drag; Zoom Out By Right Click or using App. Max Zoom Out Button.']);

% --------------------------------------------------------------------
function varargout = RecordLengthPlus_Callback(h, eventdata, handles, varargin)
global instr idn;

texthandleToolBarString = findobj(gcbf,'Tag','ToolBarString');
set(texthandleToolBarString,'String',['Attempting to Increase Record Length. Please Wait ...']);

horizLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
newhorizLen = horizLen;
k = 0;

while ((newhorizLen == horizLen) & (k < 2))
   % If 2 consecutive attempts do not produce a new Record Length it is the min.
   k = k+1; 
   fprintf(instr,['Horizontal:RecordLength ' num2str(horizLen*(2^k))]);
   newhorizLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
end;

recordLen = str2num(query(instr,'HORIZONTAL:RECORD?'));
fclose(instr);
% Make sure the size of the InputBuffer - in bytes - is sufficient.
set(instr,'InputBufferSize',recordLen*1+100);
fopen(instr);

texthandleRecordLengthString = findobj(gcbf,'Tag','RecordLengthString');
set(texthandleRecordLengthString,'String',{'Rec Length' ; tekgetunits(recordLen)});
set(texthandleToolBarString,'String',['Connected to ' idn 'Running ... Zoom In by Left Click and Drag; Zoom Out By Right Click or using App. Max Zoom Out Button.']);

% --------------------------------------------------------------------
function varargout = SampleRateMinus_Callback(h, eventdata, handles, varargin)
global instr idn;

texthandleToolBarString = findobj(gcbf,'Tag','ToolBarString');
set(texthandleToolBarString,'String',['Attempting to Decrease Sample Rate. Please Wait ...']);

samplingInterval = str2num(query(instr,'WFMOUTPRE:XINCR?'));
samplingRate = 1/samplingInterval;
newsamplingRate = samplingRate;
k = 0;

while ((newsamplingRate == samplingRate) & (k < 2))
   % If 2 consecutive attempts do not produce a new Sampling Rate it is the min.
   k = k+1;  
   fprintf(instr,['Horizontal:Main:SampleRate ' num2str(samplingRate/(2^k))]);
   newsamplingInterval = str2num(query(instr,'WFMOUTPRE:XINCR?'));
   newsamplingRate = 1/newsamplingInterval;
end;

texthandleSamplingRateString = findobj(gcbf,'Tag','SamplingRateString');
set(texthandleSamplingRateString,'String',{'Sample Rate'; [tekgetunits(newsamplingRate) 'S/s']});
set(texthandleToolBarString,'String',['Connected to ' idn 'Running ... Zoom In by Left Click and Drag; Zoom Out By Right Click or using App. Max Zoom Out Button.']);

% --------------------------------------------------------------------
function varargout = SampleRatePlus_Callback(h, eventdata, handles, varargin)
global instr idn;

texthandleToolBarString = findobj(gcbf,'Tag','ToolBarString');
set(texthandleToolBarString,'String',['Attempting to Increase Sample Rate. Please Wait ...']);

samplingInterval = str2num(query(instr,'WFMOUTPRE:XINCR?'));
samplingRate = 1/samplingInterval;
newsamplingRate = samplingRate;
k = 0;

while ((newsamplingRate == samplingRate) & (k < 2))
   % If 2 consecutive attempts do not produce a new Sampling Rate it is the min.
   k = k+1;  
   fprintf(instr,['Horizontal:Main:SampleRate ' num2str(samplingRate*(2^k))]);
   newsamplingInterval = str2num(query(instr,'WFMOUTPRE:XINCR?'));
   newsamplingRate = 1/newsamplingInterval;
end;

texthandleSamplingRateString = findobj(gcbf,'Tag','SamplingRateString');
set(texthandleSamplingRateString,'String',{'Sample Rate'; [tekgetunits(newsamplingRate) 'S/s']});
set(texthandleToolBarString,'String',['Connected to ' idn 'Running ... Zoom In by Left Click and Drag; Zoom Out By Right Click or using App. Max Zoom Out Button.']);

% --------------------------------------------------------------------
function varargout = FFTWindowInput_Callback(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = popupmenu1_Callback(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = Fig1_CreateFcn(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = popupmenuInstrumentAddress_Callback(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = StopButton_Callback(h, eventdata, handles, varargin)
set(gcbo,'UserData',1);
pushbuttonExitButton = findobj(gcbf,'Tag','ExitButton');
set(pushbuttonExitButton,'Enable','on');


% --------------------------------------------------------------------
function varargout = Fig1_ResizeFcn(h, eventdata, handles, varargin)



% --------------------------------------------------------------------
function varargout = ChannelSelection_Callback(h, eventdata, handles, varargin)
global instr;

pushbuttonChannelSelectionButton = findobj(gcbf,'Tag','ChannelSelection');
chNumber = get(pushbuttonChannelSelectionButton,'Value');
fprintf(instr,['DATA:SOURCE ' 'CH' num2str(chNumber)]);
fprintf(instr,['SELECT:' 'CH' num2str(chNumber) ' ON']);

% --------------------------------------------------------------------
function varargout = pushbuttonZoomOut_Callback(h, eventdata, handles, varargin)
set(gcbo,'UserData',1);

% --------------------------------------------------------------------
function [data,samplingRate] = tekgetcurve(instr)

    fprintf(instr,'ACQUIRE:STATE RUN');
    while query(instr,'BUSY?','%s\n','%d'); end;
    
    horizLen = query(instr,['HORIZONTAL:RECORD?'],'%s\n','%d');
    fprintf(instr,['DATA:START ' num2str(1)]);
    fprintf(instr,['DATA:STOP ' num2str(horizLen)]);
    fprintf(instr,'CURVE?');
    
    header = fscanf(instr,'%s',2);
    header1 = fscanf(instr,'%s',str2num(header(2)));
    recordLen2Transfer = horizLen;

    [data,count] = fread(instr,recordLen2Transfer,'int8');
    Curveterminator = fread(instr,1,'char');

    % get the sampling interval 
    samplingInterval = str2num(query(instr,'WFMOUTPRE:XINCR?'));
    samplingRate = 1/samplingInterval;
    % get the trigger point within the record
    indexTrigger = str2num(query(instr,'WFMOUTPRE:PT_OFF?'));
    ymult = str2num(query(instr,'WFMOUTPRE:YMULT?'));
    yoff = str2num(query(instr,'WFMOUTPRE:YOFF?'));
    yzero = str2num(query(instr,'WFMOUTPRE:YZERO?'));

    % scale the data to the correct values
    data = ymult*(data - yoff) - yzero;

flushinput(instr);
flushoutput(instr);
return;

% --------------------------------------------------------------------
function str = tekgetunits(val)

units = '';
if val < 1e-12
   str = [num2str(val*1e15) 'f'];
elseif val < 1e-9
   str = [num2str(val*1e12) 'p'];
elseif val < 1e-6
   str = [num2str(val*1e9) 'n'];
elseif val < 1e-3
   str = [num2str(val*1e6) 'u'];
elseif val < 1
   str = [num2str(val*1e3) 'm'];
elseif val < 1e3
   str = [num2str(val*1e0) ''];
elseif val < 1e6
   str = [num2str(val/1e3) 'k'];
elseif val < 1e9
   str = [num2str(val/1e6) 'M'];
else
   str = [num2str(val/1e9) 'G'];
end;   

Contact us