Digital Signal Processing. The Scientist and Engineers Guide. Steven W. Smith
.pdf
|
Chapter 26Neural Networks (and more!) |
477 |
|
100 'ITERATIVE DESIGN OF RECURSIVE FILTER |
|
||
110 |
' |
|
|
120 |
|
'INITIALIZE |
|
130 |
N% = 256 |
'number of points in FFT |
|
140 |
NP% = 8 |
'number of poles in filter |
|
150 |
DELTA = .00001 |
'perturbation increment |
|
160 |
MU = .2 |
'iteration step size |
|
170 |
DIM REX[255] |
'real part of signal during FFT |
|
180 |
DIM IMX[255] |
'imaginary part of signal during FFT |
|
190 |
DIM T[128] |
'desired frequency response (mag only) |
|
200 |
DIM A[8] |
'the "a" recursion coefficients |
|
210 |
DIM B[8] |
'the "b" recursion coefficients |
|
220 |
DIM SA[8] |
'slope for "a" coefficients |
|
230 |
DIM SB[8] |
'slope for "b" coefficients |
|
240 |
' |
|
|
250 |
GOSUB XXXX |
'mythical subroutine to load T[ ] |
|
260 |
' |
|
|
270 |
FOR P% = 0 TO NP% |
'initialize coefficients to the identity system |
|
280 |
A[P%] = 0 |
|
|
290 |
B[P%] = 0 |
|
|
300 NEXT P% |
|
|
|
310 |
A[0] = 1 |
|
|
320 |
' |
|
|
330 |
' |
'ITERATION LOOP |
|
340 |
FOR ITER% = 1 TO 100 |
'loop for desired number of iterations |
|
350 |
GOSUB 2000 |
'calculate new coefficients |
|
360 |
PRINT ITER% ENEW MU |
'print current status to video screen |
|
370 |
IF ENEW > EOLD THEN MU = MU/2 |
'adjust the value of MU |
|
380 NEXT ITER% |
|
|
|
390 |
' |
|
|
400 |
' |
|
|
410 FOR P% = 0 TO NP% |
'PRINT OUT THE COEFFICIENTS |
|
|
420 |
PRINT A[P%] B[P%] |
|
|
430 NEXT P% |
|
|
|
440 |
' |
|
|
450 END |
|
|
|
|
TABLE 26-4 |
|
|
3040 to 3080 load an impulse in the array, IMX[ ]. Next, lines 3100-3150 use this impulse as an input signal to the recursive filter defined by the current values of A[ ] and B[ ]. The output of this filter is thus the impulse response of the current system, and is stored in the array, REX[ ]. The system's frequency response is then found by taking the FFT of the impulse response, as shown in line 3170. Subroutine 1000 is the FFT program listed in Table 12-4 in Chapter 12. This FFT subroutine returns the frequency response in rectangular form, overwriting the arrays REX[ ] and IMX[ ].
Lines 3200-3250 then calculate ER, the mean squared error between the magnitude of the current frequency response, and the desired frequency response. Pay particular attention to how this error is found. The iterative action of this program optimizes this error, making the way it is defined very important. The FOR-NEXT loop runs through each frequency in the frequency response. For each frequency, line 3220 calculates the magnitude of the current frequency response from the rectangular data. In line 3230, the error at this frequency is found by subtracting the desired magnitude, T[ ], from the current magnitude, MAG. This error is then squared, and added to the
478 |
The Scientist and Engineer's Guide to Digital Signal Processing |
accumulator variable, ER. After looping through each frequency, line 3250 completes the calculation to make ER the mean squared error of the entire frequency response.
Lines 340 to 380 control the iteration loop of the program. Subroutine 2000 is where the changes to the recursion coefficients are made. The first action in this subroutine is to determine the current value of ER, and store it in another variable, EOLD (lines 2040 & 2050). After the subroutine updates the coefficients, the value of ER is again determined, and assigned to the variable, ENEW (lines 2270 and 2280).
The variable, MU, controls the iteration step size, just as in the previous neural network program. An advanced feature is used in this program: an automated adjustment to the value of MU. This is the reason for having the two variables, EOLD and ENEW. When the program starts, MU is set to the relatively high value of 0.2 (line 160). This allows the convergence to proceed rapidly, but will limit how close the filter can come to an optimal solution. As the iterations proceed, points will be reached where no progress is being made, identified by ENEW being higher than EOLD. Each time this occurs, line 370 reduces the value of MU.
Subroutine 2000 updates the recursion coefficients according to the steepest decent method: calculate the slope for each coefficient, and then change the coefficient an amount proportional to its slope. Lines 2080-2130 calculate the slopes for the "a" coefficients, storing them in the array, SA[ ]. Likewise, lines 2150-2200 calculate the slopes for the "b" coefficients, storing them in the array, SB[ ]. Lines 2220-2250 then modify each of the recursion coefficients by an amount proportional to these slopes. In this program, the proportionality constant is simply the step size, MU. No error term is required in the proportionality constant because there is only one example to be matched: the desired frequency response.
The last issue is how the program calculates the slopes of the recursion coefficients. In the neural network example, an equation for the slope was derived. This procedure cannot be used here because it would require taking the derivative across the DFT. Instead, a brute force method is applied: actually change the recursion coefficient by a small increment, and then directly calculate the new value of ER. The slope is then found as the change in ER divided by the amount of the increment. Specifically, the current value of ER is found in lines 2040-2050, and stored in the variable, EOLD. The loop in lines 2080-2130 runs through each of the "a" coefficients. The first action inside this loop is to add a small increment, DELTA, to the recursion coefficient being worked on (line 2090). Subroutine 3000 is invoked in line 2100 to find the value of ER with the modified coefficient. Line 2110 then calculates the slope of this coefficient as: (ER & EOLD ) / DELTA . Line 2120 then restores the modified coefficient by subtracting the value of DELTA.
Figure 26-13 shows several examples of filters designed using this program. The dotted line is the desired frequency response, while the solid line is the
|
Chapter 26Neural Networks (and more!) |
479 |
||
2000 |
'SUBROUTINE TO CALCULATE THE NEW RECURSION COEFFICIENTS |
|
||
2010 |
'Variables entering routine: |
A[ ], B[ ], DELTA, MU |
|
|
2020 |
'Variables exiting routine: |
A[ ], B[ ], EOLD, ENEW |
|
|
2030 |
' |
|
|
|
2040 GOSUB 3000 |
|
'FIND THE CURRENT ERROR |
|
|
2050 |
EOLD = ER |
|
'store current error in variable, EOLD |
|
2060 |
' |
|
|
|
2070 |
|
|
'FIND THE ERROR SLOPES |
|
2080 |
FOR P% = 0 TO NP% |
|
'loop through each "a" coefficient |
|
2090 |
A[P%] = A[P%] + DELTA |
|
'add a small increment to the coefficient |
|
2100 |
GOSUB 3000 |
|
'find the error with the change |
|
2110 |
SA[P%] = (ER-EOLD)/DELTA |
'calculate the error slope, store in SA[ ] |
|
|
2120 |
A[P%] = A[P%] - DELTA |
|
'return coefficient to original value |
|
2130 NEXT P% |
|
|
|
|
2140 |
' |
|
|
|
2150 |
FOR P% = 1 TO NP% |
|
'repeat process for each "b" coefficient |
|
2160 |
B[P%] = B[P%] + DELTA |
|
|
|
2170 |
GOSUB 3000 |
|
|
|
2180 |
SB[P%] = (ER-EOLD)/DELTA |
'calculate the error slope, store in SB[ ] |
|
|
2190 |
B[P%] = B[P%] - DELTA |
|
|
|
2200 NEXT P% |
|
|
|
|
2210 |
' |
|
'CALCULATE NEW COEFFICIENTS |
|
2220 |
FOR P% = 0 TO NP% |
|
'loop through each coefficient |
|
2230 |
A[P%] = A[P%] - SA[P%] * MU |
'adjust coefficients to move "downhill" |
|
|
2240 |
B[P%] = B[P%] - SB[P%] * MU |
|
|
|
2250 NEXT P% |
|
|
|
|
2260 |
' |
|
|
|
2270 GOSUB 3000 |
|
'FIND THE NEW ERROR |
|
|
2280 |
ENEW = ER |
|
'store new error in variable, ENEW |
|
2290 |
' |
|
|
|
2300 RETURN |
|
|
|
|
3000 |
'SUBROUTINE TO CALCULATE THE FREQUENCY DOMAIN ERROR |
|
||
3010 |
'Variables entering routine: |
A[ ], B[ ], T[ ] |
|
|
3020 |
'Variables exiting routine: |
ER |
|
|
3030 |
' |
|
|
|
3040 FOR I% = 0 TO N%-1 |
|
'LOAD SHIFTED IMPULSE INTO IMX[ ] |
|
|
3050 |
REX[I%] = 0 |
|
|
|
3060 |
IMX[I%] = 0 |
|
|
|
3070 NEXT I% |
|
|
|
|
3080 |
IMX[12] = 1 |
|
|
|
3090 |
' |
|
'CALCULATE IMPULSE RESPONSE |
|
3100 FOR I% = 12 TO N%-1 |
|
|
|
|
3110 |
FOR J% = 0 TO NP% |
|
|
|
3120 |
REX[I%] = REX[I%] + A[J%] * IMX[I%-J%] + B[J%] * REX[I%-J%] |
|
||
3130 |
NEXT J% |
|
|
|
3140 NEXT I% |
|
|
|
|
3150 |
IMX[12] = 0 |
|
|
|
3160 |
' |
|
'CALCULATE THE FFT |
|
3170 |
GOSUB 1000 |
|
'Table 12-4, uses REX[ ], IMX[ ], N% |
|
3180 |
' |
|
|
|
3190 |
|
|
'FIND FREQUENCY DOMAIN ERROR |
|
3200 |
ER = 0 |
|
'zero ER, to use as an accumulator |
|
3210 |
FOR I% = 0 TO N%/2 |
|
'loop through each positive frequency |
|
3220 |
MAG = SQR(REX[I%]^2 + IMX[I%]^2) |
'rectangular --> polar conversion |
|
|
3230 |
ER = ER + ( MAG - T[I%] )^2 |
|
'calculate and accumulate squared error |
|
3240 NEXT I% |
|
|
|
|
3250 |
ER = SQR( ER/(N%/2+1) ) |
|
'finish calculation of error, ER |
|
3260 |
' |
|
|
|
3270 RETURN
TABLE 26-5
482 |
The Scientist and Engineer's Guide to Digital Signal Processing |
|||||||
|
Lossless |
|
Lossy |
|
Method |
|
Group size: |
|
|
|
|
|
|||||
|
|
|
|
input |
output |
|||
|
|
|
|
|
|
|
|
|
|
run-length |
|
CS&Q |
|
CS&Q |
|
fixed |
fixed |
|
Huffman |
|
JPEG |
|
Huffman |
|
fixed |
variable |
|
delta |
|
MPEG |
|
Arithmetic |
|
variable |
variable |
|
LZW |
|
|
|
run-length, LZW |
|
variable |
fixed |
|
a. Lossless or Lossy |
|
b. Fixed or variable group size |
|||||
TABLE 27-1
Compression classifications. Data compression methods can be divided in two ways. In (a), the techniques are classified as lossless or lossy. Lossless methods restore the compressed data to exactly the same form as the original, while lossy methods only generate an approximation. In (b), the methods are classified according to a fixed or variable size of group taken from the original file and written to the compressed file.
Images transmitted over the world wide web are an excellent example of why data compression is important. Suppose we need to download a digitized color photograph over a computer's 33.6 kbps modem. If the image is not compressed (a TIFF file, for example), it will contain about 600 kbytes of data. If it has been compressed using a lossless technique (such as used in the GIF format), it will be about one-half this size, or 300 kbytes. If lossy compression has been used (a JPEG file), it will be about 50 kbytes. The point is, the download times for these three equivalent files are 142 seconds, 71 seconds, and 12 seconds, respectively. That's a big difference! JPEG is the best choice for digitized photographs, while GIF is used with drawn images, such as company logos that have large areas of a single color.
Our second way of classifying data compression methods is shown in Table 271b. Most data compression programs operate by taking a group of data from the original file, compressing it in some way, and then writing the compressed group to the output file. For instance, one of the techniques in this table is
CS&Q, short for coarser sampling and/or quantization. Suppose we are compressing a digitized waveform, such as an audio signal that has been digitized to 12 bits. We might read two adjacent samples from the original file (24 bits), discard one of the sample completely, discard the least significant 4 bits from the other sample, and then write the remaining 8 bits to the output file. With 24 bits in and 8 bits out, we have implemented a 3:1 compression ratio using a lossy algorithm. While this is rather crude in itself, it is very effective when used with a technique called transform compression. As we will discuss later, this is the basis of JPEG.
Table 27-1b shows CS&Q to be a fixed-input fixed-output scheme. That is, a fixed number of bits are read from the input file and a smaller fixed number of bits are written to the output file. Other compression methods allow a variable number of bits to be read or written. As you go through the description of each of these compression methods, refer back to this table to understand how it fits into this classification scheme. Why are JPEG and MPEG not listed in this table? These are composite algorithms that combine many of the other techniques. They are too sophisticated to be classified into these simple categories.





