Download Description of software and hardware codesign project

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Valve RF amplifier wikipedia , lookup

Music technology (electronic and digital) wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Transcript
Hardware/Software Codesign of a
Derivative Box to Detect Titration
Endpoint
Kevin Garwood
CSC 485a
1997
1 Introduction
This document is a compilation of web pages I created as part of a university course I
took in Hardware-Software Codesign I took in 1997 at the University of Victoria. For
the project, I chose to develop hardware and software versions of a derivative box,
which was used to find the titration point in acid-base chemical reactions.
A titration graph as a measure of pH value over time produces looks like an “S”
curve. It is the middle of the “S” which describes the point when all the acid
neutralises all of the base chemical in a solution.
The software solution applied numerical analysis techniques to take the derivative of
this S graph. The derivative of an S graph looks a bit like an upside down “v” where
the sides curve away from the middle. The inflection point would occur at the tip of
this graph.
The electrical signal generated by the pH meter can also be converted into a derivative
graph using a hardware circuit rather than software.
The focus of the 485a class project relates to the development of software and
hardware functional units which transform an incoming pH signal into the first
derivative curve of a titration graph. The functional units also analyse the resulting
derivative graph for inflection points. In the literature, these functional units will be
referred to as derivative boxes.
2 Software Derivative Box
Minimising the numerical error produced by the software derivative box was the
initial driving force of software development. The small range of possible y values for
the titration graphs, perhaps less than 160, and the use of division operators in
differentiation were the main problems for reducing roundoff and subtractive
cancellation errors.
During the first tests of the software, noise was a problem which caused the titrator to
prematurely end a titration. The signal spikes produced from outside electrical
interference have graph properties which are similar to those of inflection points,
which determine the duration of the experiment. The software was confusing the two
kinds of graph features. Certain heuristics were used to identify and remove spike
values from the data. The modified code significantly reduced the noise problem.
2.1 Numerical Analysis
A first derivative curve shows the rate of change in a graph. Change in a graph is
expressed by the slope equation (rise / run). Fig.1 shows how a sequence of y values
for points on a first derivative curve would appear:
Fig. 1
Slope equation: (y2 - y1)/(x2 - x1),
(y3 - y2)/(x3 - x2),
(y4 - y3)/(x4 - x3), ...
A titration graph often resembles an "S" shape. The inflection point in the "S"
represents the end of the titration. This point corresponds to the point in the first
derivative graph having the greatest y value.
Using automatic titration, calculating the sequence of values shown above can be
simplified by ignoring the denominator of each expression. Because the program
samples pH values at a constant rate, the differences between successive x values will
be the same. Removing the denominator constant removes a division operation that
could only introduce more numerical error. The sequence of y values for first
derivative points becomes: y2 - y1, y3 - y2, y4 - y3, ...
Thus data points for the first derivative curve are generated using successive
differences between mV readings. The inflection point is identified by observing the
largest of these y values.
2.2 Noise Reduction
pH signals are prone to noise and can generate titration graphs with spikes. In a
smooth titration graph, the rate of change is greatest just before the inflection point.
The program stores successively higher rates of change until the rate decreases.
Localised spikes in a graph reflect anomalous rates of change and can be mistaken for
inflection points by the software. The next two sections describe the kinds of spikes
the application can tolerate.
2.2.1 Spikes with Abnormally High and Low Y Values
Observe in Fig.2 how a spike could effect the identification of an inflection point:
Fig. 2
The rate of change increases during the spike, and decreases after the spike, causing
the software to mistake the anomaly for an inflection point.
To reduce the effect of these spikes, y values are averaged over an interval, and the
mean is used as if it were a data point on the titration graph. The derivative curve is
created by successive comparisons between mean values. Fig.3 shows how the
averaging reduces spikes but produces produces a titration graph with fewer points:
Fig. 3
The program produces a mean value for every ten readings of the digitiser. To reduce
numerical error, the average is lefta as a total rather than as a total divided by ten. The
data point for inflection point corresponds to a mean value. To obtain a more precise
time for the end point, the time for the inflection point is taken to be the midpoint of
the time interval used for the average.
2.2.2 Spikes with Abnormally High or Low Y Values
Averaging helps to remove spikes which have both abnormally high and low y values.
Fig 4 shows how a bias in magnitude of spike values can produce an average which is
also a spike:
Fig. 4
To correct this problem, each sequence of ten y values is totalled and averaged. The
average then replaces the highest and lowest y values in the interval. The old total is
adjusted by the differences between the average and each of the extreme values. This
total is then used in calculations for the first derivative curve.
2.2.3 Importance of Sampling Rate
Sampling rate is an important factor in detecting an end point in a titration. If a low
sampling rate is used for a fast titration, the calculation of end point will be imprecise.
If a high sampling rate is used for a slow titration, the program will need to be more
complex and have more memory reserved to sift through a large number of similar
values.
The software for MOAT was designed to compromise these two situations. The rate at
which the digitiser is read is greater than the rate of change that occurs in a typical
titration. However, only the 500 most recent values are retained for analysis. Only
summary values are used to build the image of the titration graph. The use of
averaging increases the quality of the data and makes an effective sampling rate
which is better aligned with the system1s ability to respond to changes in pH.
2.3 Software Approach
The software derivative box is a single procedure which is executed within a loop of
the titration process. The procedure uses three circular arrays: primary, secondary, and
tertiary. The primary array stores digitiser values. Each secondary value is a sum of
ten primary values, where extreme values in the sequence are replaced by an average
of the original ten. Each tertiary value is the sum of ten secondary values. The largest
positive difference between successive tertiary values is retained. The index of the
second tertiary value in the difference is also retained.
The inflection point is identified when the current difference is less than the largest
difference. The time of inflection is refined by using the index of the largest
difference to index the corresponding ten secondary values. The midpoint of the time
interval for the largest secondary value is the time of inflection.
After the derivative box is used, the titration process is put to sleep for 190 ms.
Assuming the code between reads of the digitiser takes 10 ms, a new primary value
will be read every 200 ms. Each secondary value will represent 2 s. Each tertiary valu
e will represent 20 s.
Example
Identifying Inflection Point
1. largest difference index * 10 = 10 is the starting index for the corresponding
sequence in the secondary array. Scan secondary[10]...secondary[19] for the
largest value. Suppose this value occurs at secondary[15].
2. inflection point occurred at primary[15 * 10 + 5(halfway)] = 155.
3. Current primary index is 299, so end point occurred at 299 - 155 = 144 points
ago.
4. Primary values are recorded every 200 ms, so endpoint occurred 28 seconds
before the last recorded time.
3 Hardware Derivative Box
The development of the hardware derivative box is described in three parts. First, the
behaviour of the hardware derivative box is explained. Second, a description of the
entire analogue circuit for the system is described. The hardware derivative box uses
one more piece of hardware than does the software derivative box. Third, the code for
interpreting the derivative values produced in the hardware derivative box is
explained.
3.1 Behaviour
The hardware derivative box produces a first derivative curve from a titration graph.
Fig.5 shows the characteristics of the graph made from the resulting signals.
Fig. 5
3.2 Description of Analogue Circuitry
Both hardware and software derivative boxes use parts of the circuit describes by Fig.
6.
Fig. 6
(Please Note: that the schematic should show 'to board' and 'from titrator' in swapped
positions). The circuit is designed in three parts:



amplifier
differentiator (for hardware derivative box)
offset
Amplifier
The incoming pH signal is first amplified by a factor of a hundred. The signals
produced by the pH meter are weak and unsuitable for the digitiser. They need to be
magnified to increase the range of values a change in pH can register 256 step
digitiser.
Differentiator
If the hardware derivative box is used, the signal is then transformed by the
differentiator, which appears in a dashed box. The 2.2 micro farad capacitor is used to
transform the signal into a derivative value. The 0.22 micro farad capacitor is optional
and may be used to reduce the noise. The software derivative box does not use the
differentiator; the amplified signal is routed directly from the amplifier to the offset.
Offset
The offset adds voltage to the signal to make all resulting values positive. An offset is
needed because the digitiser cannot interpret negative voltages with reasonable
accuracy.
3.3 Code for the Hardware Box
The spike in the derivative curve is observed as a voltage signal which currently falls
outside the range of the digitiser. Such signals will either produce 0 or 255 on the
digitiser. The software identifies the inflection point when two consecutive extreme
values have been read.
4 Hardware Software Comparison
4.1 Cost
4.1.1 Labour
Labour will not be considered as a cost factor because time estimates for building the
hardware derivative box are not known. Estimates for the time spent developing the
software derivative box are also not known. The software had to be incrementally
adjusted to respond to fine tuning of the sensors and amplifiers.
If hardware and software are closely linked in a system, it is hard to estimate the time
to develop either in isolation. Perhaps this is an area for codesign research!
4.1.2 Parts
Both the hardware and software derivative boxes require the same network of
amplifiers and offsets to magnify the weak pH signal which is generated by the pH
meter.
4.1.3 Software Version
For the development of the software derivative box, no other additional hardware
costs are incurred; the program makes use of 68HC11 memory resources which
already exist.
4.1.4 Hardware Version
The derivative box requires the addition of the differentiator shown in the schematic.
Chemistry technician Terry Wiley was consulted for price estimates for producing a
stand alone differentiator which could be inserted into the system. The following is a
price breakdown:






2.2 microfarad capacitor: $2.10
amplifier: $3.20
breadboard: $5.00
47 kiloohm resistor: $0.30
6.8 megaohm resistor: $0.11
0.22 microfarad capacitor: $0.30
Total: $11.01
Thus the replication of a hardware box unit would cost $11.00 more than the
replication of a software derivative box.
4.2 Performance
Performance is measured on the ability for the system to stop at a precalculated time
during a titration where all the variables are known. Some priliminary trials of
MOAT have been conducted.
Unfortunately, the design of the experiments lacks some quality of replication. I didn't
have enough of the same prepared solutions to conduct all four experiments. The
solutions had to be changed, which meant that the pump mechanism had to be cleaned
a nd drained. Draining the pump in a timely manner required the flow rate to be
changed.
New solutions were made, but my ability to prepare solutions is not as good as that of
a professional chemist. When the new titrant was fed into the pump, the flow rate had
to be recalibrated and was slightly different for some of the experiments.
The quality of the solutions prepared and the flow rate significantly impact on the
performance of the titration.
The results do look promising, but a standardisation of tests is required to make
further comparisons between the hardware and software derivative boxes.
Results from the four experiments conducted may be viewed at these links:


Software Version, Short Titration
Software Version, Long Titration


Hardware Version, Short Titration*
Hardware Version, Long Titration*
*not currently available. The current configuration of the circuitry is in an unstable
state and cannot be used to produce good results.
4.4 Numerical Error
The kinds of numerical error examined are errors resulting from subtractive
cancellation (error caused by subtracting two similar numbers); roundoff error from
division and roundoff error from truncation.
4.4.1 Software Version
The error caused by subtractive cancellation is negligable in the software. The steps
which determine when titration has completed rely on differences between large
values.
Roundoff error is also negligable because floating point numbers are not used. This
type of error is only found in the calculation of the time for inflection. There is a
difference between the time when inflection occurs and the time when inflection is
detected. The number of data points that are taken in the interm is divided by the
sampling rate (5 per second) to determine the number of seconds before the current
time when end point occurred.
This division provides a truncated result. The most error from this truncation occurs
when the division has a remainder of four. Four data points corresponds to a total of
0.8 seconds. For long titrations, this value becomes negligable. In future, rounding
could remedy much of this error.
4.4.2 Hardware Version
There is no error from subtractive cancellation generated by the hardware derivative
box because no subtractions are used to determine the time of inflection point.
The hardware version incurs almost no roundoff error. Numerical error is a property
of digital systems, and the derivative curve is generated by analogue means.
Furthermore, identification of the spike in the first derivative curve does not involve
any arithmetic that may incur roundoff error.
The only numerical error occurs at the sample points on the spike. When inflection
point is reached, the spike goes beyond the range of the digitiser. The magnitude of
the signal is beyond the 256 steps that make up the digital scale. The error becomes
most significant for spikes which occur over a long period of time because it is
impossible to determine a peak. It is analogous to pinpointing the top of a mountain
when the highest elevations are shrouded in cloud.
Ideally, the high voltages produced from the hardware that makes the first derivative
curve would be moderated by more amplifiers, noise filters and offsets to allow the
entire range of the spike to be reflected in the digitiser.
Currently, the software for the hardware derivative box identifies inflection point
when two successive boundary values are recorded. For long spikes, the software
would have to be modified to wait for more consecutive values. Then the midpoint in
an in terval of boundary values would serve as a good estimate for the inflection
point.
4.5 Resilience to Noise
For both versions, the effect of noise could be reduced by decreasing the sampling
rate, which is set in the loop of the titration process. A lower sampling rate would
insulate the system from spikes which occurred entirely between samplings. However,
t he lowered rate would result in less precision for identifying the end point of
titration.
4.5.1 Software Version
Only a limited statistical analysis is applied to the primary array of digitiser values to
remove anomalous data points. Spikes will reflect abnormally high or low values in a
data set. It is assumed that a spike will only cause one high and/or one low value. For
every ten digitiser values, the lowest and highest values are replaced by an average of
the ten. Only the first maximal and first minimal values are replaced; the others
remain to contribute to a total which is used in another array.
The secondary array holds values of the modified sums. It is assumed that anomalous
values won't be significant in the secondary array. However, the effect of localised
spikes is reduced by a tertiary array. Each element of this third array holds a sum of
ten secondary values.
Analysis of the tertiary array assumes that only trends reflecting the true nature of the
titration curve will be identified.
4.5.2 Hardware Version
The differentiator offers greatest response to the greatest change in voltage. Thus
because spikes show a greater change in voltage than does a smooth titration, the
differentiator preferentially amplifies noise instead of a normal signal.
The effect of the spike is dependent on the duration of the spike relative to the
propagation time of a signal through the circuit. Increasing the propagation delay in
the derivative box circuit would better insulate the system from noise, but would carry
the expense of having lower sampling rates.
4.6 Reliability
Reliability will be measured by the ability for the system to terminate operation.
4.6.1 Software Version
The software responds to rates of change in the pH of the solution in the beaker. If
there is no pH change, the software will not terminate. However, for any experiment
where the cables are plugged in and where the starting pH values of the standard and
nonstandard solutions are different, the software derivative box will terminate.
The difference in pH at the start guarantees a change in pH of the solution in the
beaker, and this change will be recorded by the software. At the end of the titration,
when pH is not changing, the change will be less positive than the change recorded
earlier, thus causing termination.
4.6.2 Hardware Version
One preliminary trial showed that the hardware derivative box would not end titration.
For experiments where the change in pH was slow, the inflection point in the graph
was not very pronounced. The magnitude of the spike was such that it didn't equal or
exceed a boundary value on the digitiser. However, the trial was isolated, and didn't
reflect the behaviour of the box for higher flow rates.
4.7 Speed
The comparisons of speed between versions is restricted to the total time used by a
version to produce and use a first derivative value to determine whether inflection
point has been reached.
4.7.1 Software Version
The execution time of the software for the software derivative box was timed using
the 68HC11 main timer. The timer counter, TCNT, is set to a default value, where the
time between ticks is 500 ns. Execution time of the software was measured by taki ng
the difference of TCNT values before and after the body of code had been executed.
On average, the difference was 350, which means the software took 350 * 500 ns =
0.175 ms to execute.
4.7.2 Hardware Version
The time used by the hardware derivative box can be divided into time used by the
circuit and time used by the software.
4.7.3 Time for the Circuit
In a resistor-capacitor circuit, capacitors account for the greatest part of propagation
time. The time is spent charging and discharging the capacitors. In the hardware
derivative box circuit, only the differentiator has a large capacitor; the other
amplifiers and resistors cause negligible delay.
The time for a signal to propagate through the differentiator circuit may be obtained
by using the formula:
t = 1 / (2*PI*Rf*C)
From the schematic, Rf = 6.8 megaohms and C = 2.2 microfarads; t is 10.6 ms.
4.7.4 Time for the Software
The software for the hardware derivative box was timed in the same fashion as the
software for the other derivative box.The difference was 46, which means it took 46 *
500 = 23, 000 ns or 0.023 ms.
Thus the total time used by the hardware derivative box is 10.623 ms.
4.7.5 Relevance of Derivative Box Speed to System Performance
Overall, the time used by either derivative box is insignificant to the time needed for a
change in pH to register with the pH meter. The time for a drop to fall into solution,
diffuse, and be detected by the pH electrode is closer to a magnitude in seco nds
rather than milliseconds.
5 Credits and Thanks




Dr. Frank Roberts for his help in developing a numerical analysis strategy for
the data.
Terry Davies for showing me limitations of sampling rate for getting better
results.
Terry Davies, Terry Wiley, and Bob Dean for building the circuit and
explaining to me aspects of the circuit operation
Terry Davies for providing me the RC circuit formula used to determine the
propagation time in the circuit for the hardware derivative box. He also
provided cost estimates for the parts needed to make the differentiator.