Digital Waveform Capture with LabView

 

The objective of this lab is to explore the capabilities of the LabView system for object-oriented program control of instruments, to record some waveforms of sound, and to carry out some basic frequency analysis of these waveforms

 

I.  Starting LabView

 

Start up one of the Windows computers with LabView and the National Instruments data-acquisition card installed (plab2.sfsu.edu, for instance).  Ideally the computer should be on the network, with a ssh link to the Department Unix system, an xterm window open on one of the Linux computers (say th123-12), and your Unix file system mounted as a DOS drive.  This way your data files can be saved on the Unix server and analyzed using MatLab on the Linux system.

 

Open the "Tools" folder on the desktop and start LabView by double-clicking on its icon. Click on "New VI." VI stands for Virtual Instrument, and files of the type vi, whose name ends with .vi, contain LabView instrument-control programs. In the window which opens, click on "Windows," then "Show Diagram." The diagram window is the most useful one for programming.

 

II.  Recording waveforms

 

In LabView, click on "Windows," then "Show Functions Palette." This displays icons for the various LabView functions (really subroutines) which you can invoke. Click around for a while and see the variety of functions available.

 

Now we will write a data-acquisition program.  In the Functions palette, find "Data Acquisition," "Analog Input," and "AI Acquire Waveform.vi". Drag it onto your diagram and drop it. This is the beginning of a program. Save it to your project-lab directory on the Unix file system, preferably to a special subdirectory for this day's lab. Call it wave1.vi.

 

Now right-click on the AI Multi Pt icon and select "Online Help." See if you can figure out how to set up this function to do the following:  Capture a waveform of 4096 points, at a digitization rate of 2000 Hz.  Display the resulting array of values in an indicator on the “instrument panel” screen.  HINT:  select the “wiring” mode (click on the icon representing a roll of wire).  Then right-click on the places to connect to the AI Multi Pt icon.  For inputs, select “Create Constant.”  For the output, select “Create Indicator.”

 

When you have this wired up correctly, run the vi (by clicking on the white arrow icon) and see what numbers come up in the indicator.  By default it is acquiring a waveform from input channel zero of the National Instruments BNC-2120 terminal board, which should be attached to your computer.  As a test signal, cable the output of the function generator on the BNC-2120 board to input channel zero, selecting a triangle wave.  Look in the output indicator and see if there are numbers which change.

 

 

As a check of the data we can display the waveform after it is recorded. To show the waveform, go to the instrument-panel window, and from the Controls palette, select Graph, Waveform Graph, and drop it onto the window.  Then go to the Diagram window and wire the output of the Ai Multi Pt icon to the Waveform Graph.  Now when you run the vi, the waveform should be graphed.  Make sure that the triangle wave is of a reasonable amplitude and has the right shape (sharp corners, straight sides).

 

III. Writing an output file

 

Now you have attained the first level of vi mastership, reading in some data.  Next we want to write it out.  Go to the Functions Palette, File I/O, Write To Spreadsheet File.vi, and drop this icon on your diagram window.  It will be used to write a file.  Ideally the file should be written directly to the Unix server.  We will assume below that you have mounted your Unix files as drive F:, and that you have a directory named ph490, with a subdirectory named labview.

 

First, you must say where to write the file.  This means giving a path and name for the file, and converting them into a “path” for the Write To Spreadsheet File subroutine.  Go to the Functions palette and select String, Conversion, and String to Path.  Drop this icon on your diagram window.  Then, in wiring mode, create a constant for the input.  This will be the file path and name, e.g.:

            F:\ph490\labview\triangle.dat

Then wire from this conversion vi to the “file path” input of the Write To Spreadsheet File vi.  [Note:  if you don’t give it a file path, it will call up a dialogue box upon execution so that you can select a file name and path.]

 

Now connect the output of the AI Multi Pt icon to the input of the Write To Spreadsheet File vi.  Finally, set its Transpose input to true; this is to make the data come out as a column of data, rather than a single very long row.

 

This should take care of writing a waveform to disk!  Run the vi, then open the output file with Excel and plot it, just to make sure that things are working.

 

Save this program (wave1.vi) in your unix files, for future use.

 

IV.  Files for further analysis

 

Now record and save some sound files.  For voice recordings, recommended settings are: 4096 points, and sampling frequencies of 4000 Hz for men, 16000 for women.  (The highest note that Pavarotti can sing is high C, 512 Hz; Joan Sutherland probably goes up to 2000 Hz.)  Then record some files:

            triangle.dat  a triangle wave; Ideally you should have about 20 samples per cycle.  Note the frequency of the waveform.

singhigh.dat  singing as high as you can.

singlow.dat  singing as low as you can

pretty.dat  singing a pretty (harmonically pure) note

ugly.dat  singing a harmonically complex note

function.dat  record the instructor’s secret function (if available)

prettylady.dat or prettyman.dat  pretty note from the opposite sex

 

NOTE:  When you close LabView, do not save changes to the system vi’s.

 

V.  Frequency Analysis

 

   Assume that waveforms to analyze are available as text files, one value per line.  These can be read in and Fourier-transformed by MatLab, as follows.

   From a computer in project lab or in the computer room, ssh to quark, open an xterm window, and from that window ssy to th123-12 and run MatLab:

            ssh quark

            xterm

            ssh th123-12

            matlab 

After a while some programming windows will open on your computer.

   Then read in the data, check that it is in memory, and plot it:

            load triangle.dat

            whos                            Let's suppose that this shows singhigh as an array 4096 long.

            plot(triangle)

Then transfer it to the array x, calculate the complex transform in array z, put the power spectrum in y, and plot it:

            x=triangle

            z=fft(x);                        Note:  the semicolon inhibits printing the result.

            y=abs(z(1:2048))          Assumes that x is 4096 long

            plot(y)

Here you should use the ability of  Matlab to do blowups on a graph on the screen to check out the waveform.

 

Now you may want to have the x-axis labeled in terms of frequency.  Here is how to do it.

            npt=4096                     Use the length of your waveforms

            samprate=4000.           Use your sampling rate.

            f=(0:npt/2-1)*samprate/npt

            plot(f,y)

 

   The main peak from the triangle wave should come at the right frequency.  The triangle wave should have only odd Fourier coefficients, and they should fall off like 1/n2, alternating in sign.  (See http://www.physics.sfsu.edu/~bland/courses/385/cprobs/waves/coeff.html for more details.)  Look at your plot of y and see if the peaks seem to agree with this expectation.  You might also look for aliasing; the higher-frequency components which are over the Nyquist frequency (half the sampling frequency) will appear in the spectrum as spurious peaks.  A peak at frequency f < fNy will appear shifted down by a multiple of the sampling frequency (negative frequencies reflect to positive frequencies).  See if you can identify some aliasing peaks.

 

   Then analyze your singing waveforms.  Can you determine what makes a sound pretty or ugly?!

 

NOTE:  When you close LabView, do not save changes to the system vi’s.

 

VI.  Spectrograms

 

   A spectrogram is a plot of “frequency versus time.”  It actually consists of a series of power spectra made for consecutive pieces of a time-series signal.  The strength of the signal is usually represented in color code.  To see a spectrogram of  the signal x that you have in memory in Matlab, enter

            specgram(x)

This is a good thing to do to a signal of someone singing, to see the notes change with time.

   To control the parameters of the spectrogram yourself, go to the MatLab help on specgram.  The full calling sequence is

            specgram(a,f,fs,window,numoverlap)

Here a is the signal to be transformed, fs gives the sampling frequency (which results in having a frequency scale on the spectrogram), window is a rather technical parameter, and noverlap is the overlap of consecutive sections of the signal to be transformed.  For instance,

            nfft=128

            specgram(x,nfft,samprate)

gives a nice spectrum with a frequency scale.  Try varying nfft and see how this changes the frequency resolution.

 

VII.  Appendix A:  The Discrete Fourier Transformation

 

The Fourier transform of a function over a finite time interval [0,T] is given by

,

where

,

and

.

When working with digitized waveforms, the values of f(t) become discrete:

.

The allowed frequencies then become

and

,

 

.

This is the discrete Fourier transform.  [The limitation of frequencies to the N values chosen requires some explanation; a short version is that the higher frequencies produce functions of time which are different from those used, but not­ ­at the discrete set of time values for which the function is known.  This equivalence of higher frequencies to lower ones is known as aliasing.]

 

   The expansion above describes an arbitrary complex function.  It is fairly easy to show that for a real function f(t),

;

 

 

 

VIII.  Equipment

 

1 – computer, with internet connection and ssh-2.

1 - cable, National Instruments 184749A-01, 1 meter, to connect interface card to connection board

1 – Connection board, National Instruments BNC-2120

1 - microphone