File: ellipfilt.C

package info (click to toggle)
mixviews 1.10-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,440 kB
  • ctags: 6,314
  • sloc: cpp: 31,647; ansic: 2,100; makefile: 1,782; sh: 17
file content (149 lines) | stat: -rw-r--r-- 4,637 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// ellipfilt.c

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <math.h>
#include "application.h"
#include "localdefs.h"
#include "ellipfilt.h"
#include "request.h"
#include "valuerequester.h"

class EllipticalRequester : public ValueRequester<double> {
public:
	EllipticalRequester(EllipticalFilter *);
protected:
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	EllipticalFilter* client;
};

EllipticalRequester::EllipticalRequester(EllipticalFilter* e)
	: ValueRequester<double>("Apply Elliptical Filter to Selected Region:",
	                         "Gain Factor:", e->gain), client(e) {}

void
EllipticalRequester::configureRequest(Request* request) {
	Range allFreqs(0.0, client->sampRate()/2.0);
	request->appendValue("Passband Cutoff (Hz.):",
			    &client->passbandCutoff, allFreqs);
	request->appendValue("StopBand Cutoff (Hz.):",
			    &client->stopbandCutoff, allFreqs);
	request->appendValue("BandPass StopBand (Hz.):",
			    &client->bandpassStopband, allFreqs);
	request->appendValue("PassBand Ripple (db.):",
			    &client->passbandRipple, Range(0.1, 12.0), true);
	request->appendValue("StopBand Attenuation (db.):",
			    &client->stopbandAttenuation, Range(0.0, 90.0), true);
	ValueRequester<double>::configureRequest(request);
}

boolean
EllipticalRequester::confirmValues() {
	if(client->bandpassStopband != 0.0 
			&& client->passbandCutoff >= client->stopbandCutoff) {
		Application::alert(
			"For bandpass mode, stop band must be > pass band.");
		return false;
	}
	return true;
}
	      
extern "C" {
int setell_(float*, float*, float*, float*, float*, float*, float*, long*);
}

EllipticalFilter::EllipticalFilter(Data* output, double pband, double stopband,
	double bpstopband, double ripple, double atten, double amp)
		: SimpleFunction(output),
		array(new float[461]), passbandCutoff(pband),
		stopbandCutoff(stopband), bandpassStopband(bpstopband),
		passbandRipple(ripple), stopbandAttenuation(atten), gain(amp) {
	initialize();
}

EllipticalFilter::EllipticalFilter(Data* output)
	: SimpleFunction(output),
		array(new float[461]), passbandCutoff(1000.0),
		stopbandCutoff(2000.0), bandpassStopband(0.0),
		passbandRipple(0.5), stopbandAttenuation(60.0), gain(1.0) {}

EllipticalFilter::~EllipticalFilter() {
	delete [] array;
}

Requester *
EllipticalFilter::createRequester() {
	return new EllipticalRequester(this);
}

void
EllipticalFilter::initialize() {
	boolean status = true;
	float sr=sampRate(), p=passbandCutoff, s=stopbandCutoff;
	float b=bandpassStopband, r=passbandRipple, a=stopbandAttenuation;
	setell_(&sr, &p, &s, &b, &r, &a, array, &numberOfSections);
	if(status)
		SimpleFunction::initialize();
}

void
EllipticalFilter::restoreState() {
	int i=0;
	for(int m=0; m < numberOfSections; m++) {
		for(int j=0;j<4;j++)  {    
			coeff[m][j] = array[i++];
			past[m][j] = 0;
		}
	}
	normalizer = array[i] * gain;
/*	char msg[64];
	sprintf(msg, "Number of filter sections: %d\n", numberOfSections);
	Application::inform(msg, true);
	sprintf(msg, "Nrmlztn. factor: %f; No. of terms: %d\n", array[i], i);
	Application::inform(msg, true);
*/
}

double
EllipticalFilter::operator () (double input) {
	register double val = input;
	for(int m=0; m<numberOfSections; m++) {
		double* p = &past[m][0];
		float* c = &coeff[m][0];
		double op = val + *c * *p + *(c+2) * *(p+1)
		       - *(c+1) * *(p+2) - *(c+3) * *(p+3);
		*(p+1) = *p;
		*p = val;
		*(p+3) = *(p+2);
		*(p+2) = op;
		val = op;
	}
	return(val*normalizer);
}