Experiment 16 Helper -- Square wave data

This notebook provides an organized way for the student to obtain the harmonic amplitude data and transfer it into CurveFit, one harmonic at a time.

This notebook concentrates on harmonics of a Square waveform; the companion Exp16 Helper 2.nb considers the Triangle waveform.

This notebook is not saveable using ctrl+S or using Save from its File menu. You must use Save As... or shift+ctrl+S.

Function to add a value to the CurveFit data set

The CurveFit data set created using this notebook will consist of (harmonic number, amplitude ratio) pairs . The amplitude ratio value is the ratio of the measured amplitude of the Fourier component for that harmonic number to its expected amplitude according to the Fourier series. This ratio should equal 1 for all harmonics in the ideal case.

The CurveFit data set is cleared by calling the existing CurveFit function EraseData[].
The following function AddData[] is called to merge a data point into the existing CurveFit data set.

Evaluate this subsection of cells!

These cells will start CurveFit (if it is not already running) and erase any existing data. They then define the function AddData[].

Needs["CurveFit`"]

EraseData[] (* Use Undo[] to recover data *)

AddData[harmonic_,ratio_,name_String]:=
Block[{tempdata ={}},
 tempdata ={{{harmonic(*x*)},{ratio(*y*)},{0(*sx*)},{0(*sy*)},1 (*number of points*)},name,"Exp. 16"<> name<>" (Harmonic, Measured/Math ratio)"};
MergeData[tempdata];
(* Now set the CurveFit dataset Y uncertainties equal to the Y sample standard deviation *)
 sy=0 sy;If [CurveFit`n > 1,sy+=StandardDeviation[yy]];
]

Frequency and Gain Values

Choose the measurement frequency for the harmonics to be near the filter’s resonance peak but with only 3 significant digits. 

Edit and evaluate the following cell:

(* Frequency where gain is measured in Khz *)
fo=60.5 ;(* kHz *)

The gain function g[Vin] will provide the measured filter gain for a signal generator RMS voltage applied to the filter input of Vin. The initial behavior of g[Vin] is undefined. 

Evaluate the following cell:

g[v_]:= Print["g["<>ToString[v]<>"] is undefined."]

Quad Voltmeter App Setup

Start the Quad Voltmeter app:    . You’ll see four windows: . You need only the upper left one, so minimize (hide) the other three. What’s left on the desktop should be (your voltage may vary):
 Hover over the various objects for tooltips regarding them.

You want to measure the harmonic filter’s output, which is an AC RMS signal on the data acquisition channel Ai1. You’ll measure over a range of up to a few volts. Change the controls settings to AC RMS, Dev1/ai1, and +/- 5V. Then you should see something similar to:
 .
Except that the voltage reading changes a couple of times per second. The Pause button does what it claims - try it!

The signal generator maintains whatever output voltage you specify to a high accuracy -- better than we can measure. Thus, you will use the signal generator selected output voltage amplitude at the input voltage of the waveform to the filter circuit. 

You will use the app’s measured voltage of the filter’s output as the measurement of a selected harmonic’s RMS amplitude.

You will use the signal generator’s selected amplitude (use mVrms or Vrms) as the input waveform’s RMS amplitude.

Setting up the signal generator

After using the Frequency Response app, the signal generator is left improperly configured for this experiment. To fix that, select
the Utility Button (bottom row), then Output Setup from the on-screen menu, the select Load from the left-most item in the on-screen menu, then Done again from the on-screen menu.

We need to define return values for the filter’s gain function g[ ] at frequency fo for all of the  signal generator output RMS amplitudes we may choose to set. To start, define a value for a signal generator amplitude of 100 mVrms:

1. Set the signal generator to the frequency fo in kHz.
2. Select the signal generator Sine waveform.
3. Set the Amplitude to 100 mVrms.     Always use RMS Voltage as the generator voltage units!

You can now define the gain function g[100.] as the filter’s output AC RMS voltage (using Quad Voltmeter app) divided by Vin, the signal generator’s selected RMS voltage. Always use a sine wave output from the signal generator when measuring the gain!
 
 For example, a 100 mVrms Sine wave from the signal generator may result in a 281.7 mV output from the filter.
 You will now define a value for g[100.]
 
  Edit the following cell with your actual measured output voltage and evaluate :

(* Gain at selected input amplitude in mVrms *)
g[100.]=280.4/100.0; (* 281.7mV RMS filter output for the 100mV RMS Sine input *)

You will need to set higher signal generator voltages for the higher harmonics whose amplitudes are much smaller. Always determine g[Vin ] with a Sine waveform set to fo . Define the resulting g[Vin ] using the new input RMS value Vin as above .

Square Wave

Common Values needed for each harmonic calculation of the Square waveform

Evaluate the following cells:

(* Coefficient value which is common to all terms in the Fourier Sine series for a Square Wave with unit amplitude. *)
Co =4./Pi

(* Factor to convert input waveform RMS to peak amplitude. *)
rms2peak = 1.

1 st Harmonic (Fundamental) measurement:

The harmonic number and the corresponding frequency for the signal generator:
Evaluate the following cell and set the signal generator frequency to its result:

h=1; (* Harmonic number *)
fset = fo/h  (* Set signal generator to this frequency *)

Signal generator RMS voltage amplitude and
Filter output AC RMS voltage measurement:

Edit the following cell with your actual input and measured output voltages and evaluate :

Vin  =100.       (* mV RMS: Square wave amplitude *)
Vout=254.02  (* mV RMS: Filter output voltage *)

We next convert Vout to peak voltage using Sqrt[2], multiply by harmonic number h to take out the 1/n in the Fourier term, and divide by the appropriate filter gain value to refer back to the input. 
Evaluate the following cell:

Vcorrected =Vout Sqrt[2.] h/g[1.0 Vin]

Vcorrected should be Co times the peak amplitude of the input waveform. Peak amplitude of the input waveform is rms2peak times the signal generator RMS amplitude Vin. The ratio of Vcorrected to this value should then be Co, so divide by Co to make the result equal 1. 
Evaluate the cell:

Vratio=Vcorrected/(rms2peak Vin)/Co

Finally, if the above result seems reasonable, evaluate the next cell to add the data point to CurveFit:

AddData[h,Vratio,"Square Wave Data"]
(* Use the Undo button on the Data Viewer window if it doesn't look right *)

3rd Harmonic

The harmonic number and the corresponding frequency for the signal generator.
Evaluate the following cell and set the signal generator frequency to its result:

h=3; (* Harmonic number *)
fset = fo/h  (* Set signal generator to this frequency *)

Signal generator RMS voltage amplitude and
Filter output AC RMS voltage measurement.
Edit the following cell with your actual input and measured output voltages and evaluate :

Vin  =100. (* mV RMS: Square wave amplitude *)
Vout=84.41  (* mV RMS: Filter output voltage *)

Follow the same arithmetic as for the 1st harmonic.
Evaluate:

Vcorrected =Vout Sqrt[2.] h/g[1.0 Vin]

Vratio=Vcorrected/(rms2peak Vin)/Co

Check that result seems reasonable and, if so, evaluate the next cell to add the data point to CurveFit:

AddData[h,Vratio,"Square Wave Data"]
(* Use the Undo button on the Data Viewer window if it doesn't look right *)

5th Harmonic

Ditto for this harmonic:

h=5; (* Harmonic number *)
fset = fo/h  (* Set signal generator to this frequency *)

Edit Vin and Vout values and evaluate the cell:

Vin  =100. (* mV RMS: Square wave amplitude *)
Vout=50.62  (* mV RMS: Filter output voltage *)

Vcorrected =Vout Sqrt[2.] h/g[1.0 Vin] 
Vratio=Vcorrected/(rms2peak Vin)/Co

Evaluate the next cell to add the data point to CurveFit:

AddData[h,Vratio,"Square Wave Data"]
(* Use the Undo button on the Data Viewer window if it doesn't look right *)

Changing input amplitude and measuring the resulting Gain Value

You will use higher signal generator voltages for higher harmonics where the output is smaller . Determine gain with a Sine waveform set to fo . Name the resulting gain value using the input RMS value as for g[100.] .

(* Frequency where gain is measured in Khz *)
fo

Set the signal generator to the Sine waveform and its frequency to fo. Enter a different RMS amplitude value. You might need to change the voltage range on the Voltmeter app to avoid overflow. 

Edit and evaluate:

(* In this example, we set 500mV RMS, so we define the new g[500.] *)
g[500.]=1.4211 /0.500 (* output volts on Voltmeter app / Signal gen. RMS Volts *)

11th Harmonic using a different signal generator voltage and newly-defined gain value

The harmonic number and the corresponding frequency for the signal generator:

h=11; (* Harmonic number *)
fset = fo/h  (* Set signal generator to this frequency *)

New signal generator RMS voltage amplitude and the filter’s output AC RMS voltage measurement:

Vin  =500. (* 500mV RMS Square wave amplitude for this example *)
Vout=115.02  (* mV RMS: Corresponding Filter output voltage from Voltmeter app *)

Vcorrected =Vout Sqrt[2.] h/g[1.0 Vin] 
Vratio=Vcorrected/(rms2peak Vin)/Co

Evaluate the next cell to add the data point to CurveFit:

AddData[h,Vratio,"Square Wave Data"]
(* Use the Undo button on the Data Viewer window if it doesn't look right *)

nth Harmonic (copy, edit, and evaluate to add more Square wave harmonics)

The harmonic number and the corresponding frequency for the signal generator:

h=15; (* Harmonic number example *)
fset = fo/h  (* Set signal generator to this frequency *)

New signal generator RMS voltage amplitude and the filter’s output AC RMS voltage measurement:

Vin  =500. (* 500mV RMS Square wave amplitude for this example *)
Vout=84.35  (* mV RMS: Corresponding Filter output voltage from Voltmeter app *)

Vcorrected =Vout Sqrt[2.] h/g[1.0 Vin] 
Vratio=Vcorrected/(rms2peak Vin)/Co

Evaluate the next cell to add the data point to CurveFit:

AddData[h,Vratio,"Square Wave Data"]
(* Use the Undo button on the Data Viewer window if it doesn't look right *)

Copy the above cell Subsection grouping and edit the values for h, Vin and Vout for other harmonics.

Fitting the data:

Evaluate the following cells to have CurveFit fit your data points:

LinearDataPlot[ImageSize->400]

All data points ideally should have y = 1.0, so fitting to a constant is appropriate:

ConstantFit[]

Because our code sets the Y sigmas to the standard deviation, the reduced Chi-squared will be identically 1.0

LinearDifferencePlot[ImageSize->400]

Saving the CurveFit data set!!

What follows is the command you get from the CurveFit main palette selection Data I/O : Save File.
Give the filename a .dat extension (SquareData.dat, perhaps?). It will generate a standard CurveFit data file.
Evaluate the following cell to save your CurveFit data set:

With[  {name = SystemDialogInput["FileSave", DataFileName]},
  If[ name =!= $Canceled, SaveFile[name]]
]