File: make_filter.m

package info (click to toggle)
libsamplerate 0.2.1%2Bds0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,448 kB
  • sloc: ansic: 371,374; sh: 121; makefile: 108
file content (134 lines) | stat: -rw-r--r-- 4,113 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function f = make_filter (cycles, increment, atten, filename)

# This works :
# 
# f = make_filter (67, 128, 100.3) ;
# f = make_filter (13, 128, 100.5) ;
# f = make_filter (185, 4, 157.0) ;


#=======================================================================

if nargin < 3,
	error ('Try make_filter (12, 32, 88, "output.txt")') ;
	endif

if nargin < 4,
	filename = 0 ;
elseif (isstr (filename) == 0),
	error ("Fourth parameter must be a file name.") ;
	endif

fudge_factor1 = 1.0 ;
f1 = generate_filter (cycles, fudge_factor1, increment, atten) ;
[stop_atten stop_band_start1 minus_3db] = measure_filter (f1, atten) ;
printf ("    fudge_factor : %15.13f   stop_band_start : %15.13f   1\n", fudge_factor1, stop_band_start1) ;

fudge_factor2 = 1.25 ;
f2 = generate_filter (cycles, fudge_factor2, increment, atten) ;
[stop_atten stop_band_start2 minus_3db] = measure_filter (f2, atten) ;
printf ("    fudge_factor : %15.13f   stop_band_start : %15.13f   2\n", fudge_factor2, stop_band_start2) ;

f = f1 ;
fudge_factor = fudge_factor1 ;
stop_band_start = stop_band_start1 ;

while ((stop_band_start1 - stop_band_start2) > 0.0000000001)
	if (stop_band_start1 < stop_band_start2)
		printf ("stop_band_start1 < stop_band_start2\n") ;
		break ;
		endif

	fudge_factor = fudge_factor1 + (fudge_factor2 - fudge_factor1) / 2 ;
	f = generate_filter (cycles, fudge_factor, increment, atten) ;
	[stop_atten stop_band_start minus_3db] = measure_filter (f, atten) ;
	
	if (stop_band_start > 1.0)
		printf ("A %10.8f   %10.8f   %10.8f\n", fudge_factor1, fudge_factor, fudge_factor2) ;
		continue ;
		endif

	if (stop_band_start < 0.5 / increment)
		f2 = f ;
		stop_band_start2 = stop_band_start ;
		fudge_factor2 = fudge_factor ;
		choice = 2 ;
	else
		f1 = f ;
		stop_band_start1 = stop_band_start ;
		fudge_factor1 = fudge_factor ;
		choice = 1 ;
		endif

	printf ("    fudge_factor : %15.13f   stop_band_start : %15.13f   %d\n", fudge_factor, stop_band_start, choice) ;
	endwhile

printf ("\n") ;

#-------------------------------------------------------------------------------
# Grab only half the coefficients.

N = length (f) ;

f = increment * f' ;

if rem (length (f), 2) == 0,
	index = find (f == max (f)) ;
	index = min (index) - 1 ;
	half_f = f (index:length (f)) ;
else
	error ("Length should be even.") ;
	endif

trailing_zeros = 4 - rem (length (half_f), 4) ;

#-------------------------------------------------------------------------------
# Print analysis.

printf ("# f = make_filter (%d, %d, %4.1f) ;\n", cycles, increment, atten) ;
printf ("#   Coeff. count     : %d\n", N) ;
printf ("#   Fudge factor     : %9.7f\n", fudge_factor) ;
printf ("#   Pass band width  : %12.10f (should be %12.10f)\n", stop_band_start, 0.5 / increment) ;
printf ("#   Stop band atten. : %5.2f dB\n", abs (stop_atten)) ;
printf ("#   -3dB band Width  : %5.3f\n", 0.5 / increment / minus_3db) ;
printf ("#   half length      : %d\n", length (half_f) + trailing_zeros) ;
printf ("#   increment        : %d\n", increment) ;

if filename,
	file = fopen (filename, "w") ;
	if file == 0,
		str = sprintf ("Error, not able to open '%s'", filename)
		error (str) ;
		endif

	fprintf (file, "/*\n") ;
	fprintf (file, "**  f = make_filter (%d, %d, %4.1f) ;\n", cycles, increment, atten) ;
	fprintf (file, "**  Pass band width  : %9.7f (should be %9.7f)\n", stop_band_start, 0.5 / increment) ;
	fprintf (file, "**  Stop band atten. : %5.2f dB\n", abs (stop_atten)) ;
	fprintf (file, "**  -3dB band width  : %5.3f\n", 0.5 / increment / minus_3db) ;
	fprintf (file, "**  half length      : %d\n", length (half_f)) ;
	fprintf (file, "**  increment        : %d\n", increment) ;
	fprintf (file, "*/\n\n") ;

	for val = half_f,
		fprintf (file, "% 24.20e,\n", val) ;
		endfor

	if trailing_zeros > 0,
		for val = 2:trailing_zeros,
			fprintf (file, " 0,\n") ;
			endfor
		fprintf (file, " 0\n") ;
		endif

	fclose (file) ;
	endif

endfunction

# Do not edit or modify anything in this comment block.
# The arch-tag line is a file identity tag for the GNU Arch 
# revision control system.
#
# arch-tag: 2f1ff4fa-ea6a-4e54-a5f8-dad55def9834