10    ! FREQRESP
20    !-------------------------------------------------------------------------
30    ! HPBASIC/IBASIC Program: Freq Response
40    ! This program puts the instrument in a 2 channel mode, sets up a
50    ! frequency response measurement, runs the measurement and then
60    ! transfers the data. This program also distinguishes between IBASIC
70    ! and HPBASIC run modes.
80    !
90    ! NOTE: for best results connect the source and channel to the input of
100   !       the device under test, and channel 2 to the output of the device
110   !       under test. You could also use a straight connection between
120   !       both inputs and the source.
130   !-------------------------------------------------------------------------
140   !
150   ON ERROR GOTO Not_ibasic     ! auto selection of proper select code
160   Device=800                   ! device select code from IBASIC
170   CLEAR Device
180   GOTO Start_prog
190   !
200 Not_ibasic: !
210   Device=711                   ! device select code from external computer
220 Start_prog: !
230   OFF ERROR
240   !
250   !
260   ASSIGN @Analyzer TO Device   ! allows code to be address independent
270   !
280   !
290   ! set aside a large space for the data
300   DIM A(5000)
310   !
320   OUTPUT @Analyzer;"*RST"            ! reset the analyzer
330   OUTPUT @Analyzer;"INP2 on"         ! put the instrument in 2 channel
340   OUTPUT @Analyzer;"OUTP ON"         ! Turn on the source output
350   OUTPUT @Analyzer;"SOUR:FUNC RAND"  ! Set the source to random noise
360   OUTPUT @Analyzer;"SOUR:VOLT 1 VPK" ! Set the source level
370   OUTPUT @Analyzer;"AVER ON"         ! Turn on the default averaging
380   OUTPUT @Analyzer;"ABOR;:INIT; *WAI"! Start a new measurement
390   OUTPUT @Analyzer;":INIT:CONT OFF"  ! pause after first measurement
400   !
410   ! assign an i/o path for binary transfers
420   ASSIGN @Analyzer_bin TO Device;FORMAT OFF
430   !
440   ! command the analyzer to output block data in floating point binary
450   ! representation.
460   OUTPUT @Analyzer;"form:data real,64"
470   !
480   ! display frequency response data in trace A
490   OUTPUT @Analyzer;"CALC1:FEED 'XFR:POW:RAT 2,1'; *WAI"
500   !
510   ! store the frequency response data in data register 1
520   OUTPUT @Analyzer;"TRAC:DATA D1,TRAC1"
530   !
540   ! read the data from data register 1.  since were reading through
550   ! TRAC and not CALC we'll be getting the real and imaginary
560   ! values from the analyzer and not the values shown on the
570   ! display.
580   OUTPUT @Analyzer;"TRAC:DATA? D1"
590   !
600   ! read the header of the data block.  first read the '#n' to
610   ! find out how many digits are in the byte count then read the
620   ! number of bytes.
630   ENTER @Analyzer USING "%,A,D";A$,Digits
640   ENTER @Analyzer USING "%,"&VAL$(Digits)&"D";Num_of_bytes
650   !
660   Num_points=Num_of_bytes DIV 8 ! 8 data bytes per number
670   !
680   ! Use the number of bytes to determine the amount of data.
690   ! Redimension the array so it's the same size as the data block
700   ! being read in so we can read the data in the fastest possible way.
710   REDIM A(Num_points-1)
720   !
730   ENTER @Analyzer_bin;A(*)
740   ENTER @Analyzer;A$        ! read the line feed character (IEEE 488.2)
750   !
760   ! open up a display window if in IBASIC on the analyzer
770   IF Device=800 THEN OUTPUT @Analyzer;"DISP:PROG LOW"
780   !
790   ! print the first ten real and imaginary pairs
800   DIM I$[80]
810   I$="""Data point "", 2D, ""  Re: "", MD.DDESZZ, ""  Im: "", MD.DDESZZ"
820   FOR I=0 TO 9
830     PRINT USING I$;I;A(I*2);A(I*2+1)
840   NEXT I
850   END
