| 1 |
function f = make_filter (cycles, increment, atten, filename) |
| 2 |
|
| 3 |
# This works : |
| 4 |
# |
| 5 |
# f = make_filter (67, 128, 100.3) ; |
| 6 |
# f = make_filter (13, 128, 100.5) ; |
| 7 |
# f = make_filter (185, 4, 157.0) ; |
| 8 |
|
| 9 |
|
| 10 |
#======================================================================= |
| 11 |
|
| 12 |
if nargin < 3, |
| 13 |
error ('Try make_filter (12, 32, 88, "output.txt")') ; |
| 14 |
endif |
| 15 |
|
| 16 |
if nargin < 4, |
| 17 |
filename = 0 ; |
| 18 |
elseif (isstr (filename) == 0), |
| 19 |
error ("Fourth parameter must be a file name.") ; |
| 20 |
endif |
| 21 |
|
| 22 |
fudge_factor1 = 1.0 ; |
| 23 |
f1 = generate_filter (cycles, fudge_factor1, increment, atten) ; |
| 24 |
[stop_atten stop_band_start1 minus_3db] = measure_filter (f1, atten) ; |
| 25 |
printf (" fudge_factor : %15.13f stop_band_start : %15.13f 1\n", fudge_factor1, stop_band_start1) ; |
| 26 |
|
| 27 |
fudge_factor2 = 1.25 ; |
| 28 |
f2 = generate_filter (cycles, fudge_factor2, increment, atten) ; |
| 29 |
[stop_atten stop_band_start2 minus_3db] = measure_filter (f2, atten) ; |
| 30 |
printf (" fudge_factor : %15.13f stop_band_start : %15.13f 2\n", fudge_factor2, stop_band_start2) ; |
| 31 |
|
| 32 |
f = f1 ; |
| 33 |
fudge_factor = fudge_factor1 ; |
| 34 |
stop_band_start = stop_band_start1 ; |
| 35 |
|
| 36 |
while ((stop_band_start1 - stop_band_start2) > 0.0000000001) |
| 37 |
if (stop_band_start1 < stop_band_start2) |
| 38 |
printf ("stop_band_start1 < stop_band_start2\n") ; |
| 39 |
break ; |
| 40 |
endif |
| 41 |
|
| 42 |
fudge_factor = fudge_factor1 + (fudge_factor2 - fudge_factor1) / 2 ; |
| 43 |
f = generate_filter (cycles, fudge_factor, increment, atten) ; |
| 44 |
[stop_atten stop_band_start minus_3db] = measure_filter (f, atten) ; |
| 45 |
|
| 46 |
if (stop_band_start > 1.0) |
| 47 |
printf ("A %10.8f %10.8f %10.8f\n", fudge_factor1, fudge_factor, fudge_factor2) ; |
| 48 |
continue ; |
| 49 |
endif |
| 50 |
|
| 51 |
if (stop_band_start < 0.5 / increment) |
| 52 |
f2 = f ; |
| 53 |
stop_band_start2 = stop_band_start ; |
| 54 |
fudge_factor2 = fudge_factor ; |
| 55 |
choice = 2 ; |
| 56 |
else |
| 57 |
f1 = f ; |
| 58 |
stop_band_start1 = stop_band_start ; |
| 59 |
fudge_factor1 = fudge_factor ; |
| 60 |
choice = 1 ; |
| 61 |
endif |
| 62 |
|
| 63 |
printf (" fudge_factor : %15.13f stop_band_start : %15.13f %d\n", fudge_factor, stop_band_start, choice) ; |
| 64 |
endwhile |
| 65 |
|
| 66 |
printf ("\n") ; |
| 67 |
|
| 68 |
#------------------------------------------------------------------------------- |
| 69 |
# Grab only half the coefficients. |
| 70 |
|
| 71 |
N = length (f) ; |
| 72 |
|
| 73 |
f = increment * f' ; |
| 74 |
|
| 75 |
if rem (length (f), 2) == 0, |
| 76 |
index = find (f == max (f)) ; |
| 77 |
index = min (index) - 1 ; |
| 78 |
half_f = f (index:length (f)) ; |
| 79 |
else |
| 80 |
error ("Length should be even.") ; |
| 81 |
endif |
| 82 |
|
| 83 |
trailing_zeros = 4 - rem (length (half_f), 4) ; |
| 84 |
|
| 85 |
#------------------------------------------------------------------------------- |
| 86 |
# Print analysis. |
| 87 |
|
| 88 |
printf ("# f = make_filter (%d, %d, %4.1f) ;\n", cycles, increment, atten) ; |
| 89 |
printf ("# Coeff. count : %d\n", N) ; |
| 90 |
printf ("# Fudge factor : %9.7f\n", fudge_factor) ; |
| 91 |
printf ("# Pass band width : %12.10f (should be %12.10f)\n", stop_band_start, 0.5 / increment) ; |
| 92 |
printf ("# Stop band atten. : %5.2f dB\n", abs (stop_atten)) ; |
| 93 |
printf ("# -3dB band Width : %5.3f\n", 0.5 / increment / minus_3db) ; |
| 94 |
printf ("# half length : %d\n", length (half_f) + trailing_zeros) ; |
| 95 |
printf ("# increment : %d\n", increment) ; |
| 96 |
|
| 97 |
if filename, |
| 98 |
file = fopen (filename, "w") ; |
| 99 |
if file == 0, |
| 100 |
str = sprintf ("Error, not able to open '%s'", filename) |
| 101 |
error (str) ; |
| 102 |
endif |
| 103 |
|
| 104 |
fprintf (file, "/*\n") ; |
| 105 |
fprintf (file, "** f = make_filter (%d, %d, %4.1f) ;\n", cycles, increment, atten) ; |
| 106 |
fprintf (file, "** Pass band width : %9.7f (should be %9.7f)\n", stop_band_start, 0.5 / increment) ; |
| 107 |
fprintf (file, "** Stop band atten. : %5.2f dB\n", abs (stop_atten)) ; |
| 108 |
fprintf (file, "** -3dB band width : %5.3f\n", 0.5 / increment / minus_3db) ; |
| 109 |
fprintf (file, "** half length : %d\n", length (half_f)) ; |
| 110 |
fprintf (file, "** increment : %d\n", increment) ; |
| 111 |
fprintf (file, "*/\n\n") ; |
| 112 |
|
| 113 |
for val = half_f, |
| 114 |
fprintf (file, "% 24.20e,\n", val) ; |
| 115 |
endfor |
| 116 |
|
| 117 |
if trailing_zeros > 0, |
| 118 |
for val = 2:trailing_zeros, |
| 119 |
fprintf (file, " 0,\n") ; |
| 120 |
endfor |
| 121 |
fprintf (file, " 0\n") ; |
| 122 |
endif |
| 123 |
|
| 124 |
fclose (file) ; |
| 125 |
endif |
| 126 |
|
| 127 |
endfunction |
| 128 |
|
| 129 |
# Do not edit or modify anything in this comment block. |
| 130 |
# The arch-tag line is a file identity tag for the GNU Arch |
| 131 |
# revision control system. |
| 132 |
# |
| 133 |
# arch-tag: 2f1ff4fa-ea6a-4e54-a5f8-dad55def9834 |
| 134 |
|