High Pass (Audio) Filter implemented in Java

  • Thread starter Scott Soderlund
  • Start date
S

Scott Soderlund

Hi,
I just started working on a project where the computer captures
samples of someone's speech. Each of these samples is run through a
50 Hz High Pass Filter to improve the quality of the sample. I've
done a lot of reading about High Pass filters and think I understand
pretty well how they work. What I don't understand though is a couple
of the variables in the program where the filter is actually
implemented. Perhaps someone out there can help tell me what they
mean.

Code follows and then my questions...

public class HighPass50
{
static int NZEROS = 5;
static int NPOLES = 5;
static double GAIN = 1.095980088e+00;

double xv[] = new double[NZEROS+1];
double yv[] = new double[NPOLES+1];

public HighPass50() {}

public void process(short data[])
{
for (int i=0; i <data.length; ++i)
{
xv[0] = xv[1];
xv[1] = xv[2];
xv[2] = data / GAIN;
yv[0] = yv[1];
yv[1] = yv[2];
yv[2] = (xv[0] + xv[2]) - 2 * xv[1] + ( -0.9479259375 *
yv[0]) + ( 1.9469976496 * yv[1]);
data = (short)yv[2];
}
}
}


Looking at the method above, I don't understand the meaning of the
'GAIN' variable (and more specifically the number it is set to). Is
this likely the cutoff frequency? Also, in the next to the last line
of the method, where yv[2] = ..., there are two numbers in this line
that I have no idea what they mean. The number "-0.9479259375" and
the number "1.9469976496". My guess is that the number
"-0.9479259375" is the lowest frequency possible (zero Hz) and that
"1.9469976496" is the highest frequency possible. This would make
sense if the GAIN variable was the cutoff frequency. Again, that is
my best guess! Can someone help with my understanding???

Thanks in advance,

Scott
 
S

Stan Goodman

Hi,
I just started working on a project where the computer captures
samples of someone's speech. Each of these samples is run through a
50 Hz High Pass Filter to improve the quality of the sample. I've
done a lot of reading about High Pass filters and think I understand
pretty well how they work. What I don't understand though is a couple
of the variables in the program where the filter is actually
implemented. Perhaps someone out there can help tell me what they
mean.

Code follows and then my questions...

public class HighPass50
{
static int NZEROS = 5;
static int NPOLES = 5;
static double GAIN = 1.095980088e+00;

double xv[] = new double[NZEROS+1];
double yv[] = new double[NPOLES+1];

public HighPass50() {}

public void process(short data[])
{
for (int i=0; i <data.length; ++i)
{
xv[0] = xv[1];
xv[1] = xv[2];
xv[2] = data / GAIN;
yv[0] = yv[1];
yv[1] = yv[2];
yv[2] = (xv[0] + xv[2]) - 2 * xv[1] + ( -0.9479259375 *
yv[0]) + ( 1.9469976496 * yv[1]);
data = (short)yv[2];
}
}
}


Looking at the method above, I don't understand the meaning of the
'GAIN' variable (and more specifically the number it is set to). Is
this likely the cutoff frequency? Also, in the next to the last line


Gain has nothing to do with frequency; it is amplification (whether
greater or less than unity, which would mean unchanged).
of the method, where yv[2] = ..., there are two numbers in this line
that I have no idea what they mean. The number "-0.9479259375" and
the number "1.9469976496". My guess is that the number
"-0.9479259375" is the lowest frequency possible (zero Hz) and that
"1.9469976496" is the highest frequency possible. This would make
sense if the GAIN variable was the cutoff frequency.

If data[1] is the magnitude of a sample (or the signal), then xv[2] is
the magnitude normalized by the gain, which means it still represents
the magnitude. The yv[2] is an output magnitude calculated by
weighting the magnitude of a given input sample and of the samples
before and after it. In other words, the computation is not taking
place in the frequency domain at all but in the time domain; it is the
weighting function that provides the filtering action. The terms with
the funny numbers bring into the weighting function also the two
preceding values of the output, and the funny numbers themselves
represent the respective weights to be given to those preceding
values..

--
Stan Goodman, Qiryat Tiv'on, Israel

Please replace "SPAM-FOILER" with "sgoodman".

200 years of European fecklessness in the face of Arab terror: Tripoli
Pirates (1814); OPEC Oil (1973); Saddam Hussein and Yasser Arafat
(1990 et seq.) -- but actually financing it, and marching in support
of tyranny, are 21st-century craven European wrinkles.
 
D

Dale King

Scott Soderlund said:
Hi,
I just started working on a project where the computer captures
samples of someone's speech. Each of these samples is run through a
50 Hz High Pass Filter to improve the quality of the sample. I've
done a lot of reading about High Pass filters and think I understand
pretty well how they work. What I don't understand though is a couple
of the variables in the program where the filter is actually
implemented. Perhaps someone out there can help tell me what they
mean.

Code follows and then my questions...

public class HighPass50
{
static int NZEROS = 5;
static int NPOLES = 5;
static double GAIN = 1.095980088e+00;

double xv[] = new double[NZEROS+1];
double yv[] = new double[NPOLES+1];

public HighPass50() {}

public void process(short data[])
{
for (int i=0; i <data.length; ++i)
{
xv[0] = xv[1];
xv[1] = xv[2];
xv[2] = data / GAIN;
yv[0] = yv[1];
yv[1] = yv[2];
yv[2] = (xv[0] + xv[2]) - 2 * xv[1] + ( -0.9479259375 *
yv[0]) + ( 1.9469976496 * yv[1]);
data = (short)yv[2];
}
}
}


Looking at the method above, I don't understand the meaning of the
'GAIN' variable (and more specifically the number it is set to).


The gain variable is just an attenuation for the entire signal. Basically
every value from the data array is divided by this value.
Is
this likely the cutoff frequency? Also, in the next to the last line
of the method, where yv[2] = ..., there are two numbers in this line
that I have no idea what they mean. The number "-0.9479259375" and
the number "1.9469976496". My guess is that the number
"-0.9479259375" is the lowest frequency possible (zero Hz) and that
"1.9469976496" is the highest frequency possible. This would make
sense if the GAIN variable was the cutoff frequency. Again, that is
my best guess! Can someone help with my understanding???



For some background on digital filters see:

http://www.embedded.com/story/OEG20021119S0020
http://hitchcock.dlt.asu.edu/media3/a_spanias/dsp-lect1-10/pdf files/acr-d
sp-lect-16.pdf


Digital filters work by combining weighted combinations of the the current
input sample and previous samples to produce an output. Using different
weights or coefficients gives the filter different responses.

There are finite impulse response (FIR) filters and infinite impulse
response (IIR) filters. A FIR filter simply gives a weighted average of some
number of the latest samples. An IIR also adds in a weighted average of some
number of the last outputs.

What you have here is an IIR filter because you are averaging both the last
3 samples and the last 2 previous outputs.

So in general for an IIR you have:

y(n) = sum of i = 0 to L of ( ai x(n - i) ) - sum of i = 1 to M of ( bi
y( n - i) )

As you can see that is a recursive function.

In your case we have L = M = 2 meaning that your filter is a 2nd order
filter. xv[2] corresponds to x(n) and yv[2] to y(n). xv[1] corresponds to
x(n - 1) and so forth.

Your coefficients for the filter are:

a0 = a2 = 1
a1 = -2
b1 = -1.9469976496
b2 = 0.9479259375

(In reality the GAIN parameter turns a0 and a1 into 0.912425335 and a2
into -1.8248507, but that has no effect on the frequency response, it just
attentuates the output).

These coefficients determine the response. Note that the frequency response
is dependent on the sampling rate of the data. It is impossible to say what
the cutoff frequency is without knowing the sample rate.

The coefficients don't really easily translate into anything that explains
why those values were chosen. Most likely the designer of the filter plugged
in the filter characteristcs he wanted into a program and the program spit
out the values. Or they arrived at the values through trial and error.

If you go to http://www.digitalfilter.com/iirfres.html there is an applet
where you can plug in the coefficients and see the frequency response.
Plugging in your coefficients it looks like a pretty good filter to me with
a sharp knee and flat passband. Whether the cutoff frequency is 50Hz depends
on what the sample rate is.

Plugging parameters into a filter design applet, requesting a 50Hz, 2nd
order highpass filter, I get numbers close to those values. The exact values
once again depend on the sample rate of the data and what quality of filter
you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top