Joined: 07 Dec 2005 Posts: 14 Location: iran- tehran
Posted: Thu Dec 08, 2005 12:09 am Post subject: فيلتر ديجيتال در ويژوال بيسيك به چه صورتي پياده سازي ميشه
سلام
منظور از dll در زبانهاي برنامه نويسي چيه؟ و اينكه پياده سازي ديجيتال فيلترها در ويژوال بيسيك به چه صورتيه و اگر پروژه اي در اين رابطه هست ممنون ميشم معرفي كنيد
Dynamic Link Library . بيشتر براي اين به كار ميره كه برنامه شما سورسش نمايش داده نشه . اما قابليت استفاده براي كاربران (برنامه نويس ) هاي ديگه رو هم داشته باشه .
مثلا من يه برنامه مينويسم(بصورت تابعي ) كه ميتونم باهاش به پورت پرينتر وصل بشم . اما چون نميخوام سورسش رو به كسي بدم برنامه رو dll ميكنم و به طرف ميدم . طرف هم dll رو بصورت تابع ميبينه .
وقتي اوني كه ميخواد از اين dll استفاده كنه من بهش ميگم كه اين ارگومان و اين ارگومان ديگه رو ميفرستي به اين dll بعد dll من ارتباط رو برقرار ميكنه .
اين يكي از روشهاي استفاده از dll است كه بيشتر به vb مرتبط ميشه .
مثلا خود ويندوز هم فايل dll داره كه ميشه ازشون استفاده كرد ...
بماند .
اما در مورد فيلتر ديجيتال منظورت چه فيلتريه ؟
بيشتر توضيح بده .
Joined: 07 Dec 2005 Posts: 14 Location: iran- tehran
Posted: Thu Dec 08, 2005 3:31 pm Post subject:
خيلي واضح جواب داديد مرسي
ديجيتال فيلترها در واقع فيلترهاي نرم افزاريند كه يا در ميكرو كنترلرها يا در pc قابل پياده سازيند
در واقع سيگنال ارسالي به ميكرو از طريق A/D يا ارسالي به كامپيوتر از طريق پورت سريال پس از ورود با توجه به تعريف برنامه طراح فيلتر و بازيابي ميشوند
در واقع با استفاده از تابع ضرايب تابع تبديل و تبديل z اين فيلتر عمل ميكنه دقتش هم به تعداد نمونه هايي هست كه ميگيريم
مثلا يك سينوسي رو با ديجيتا ل كردن بصورت نقطه هايي با دامنه هاي مشخص تبديل بعد با توجه به فركانسمون ضرايب رو بدست مياريم اين نقطه ها رو با يك سري عمليات محاسباتي (با توجه به اون ضرايب)تغيير و دوباره با يك D/A بصورت آنالوگ فيلتر شده در مياريم. در واقع مزيت آن هزينه كم اون در تعداد انبوه و اينكه اگر تغييري در طراحي بوجود آمد ديگر نيازي به تغيير سخت افزار نيست و فقط پروگرام كردن مجدد آن نياز ميشه.
توي مطلب به راحتي با دو دستور قابل اجراست .ولي با vb بايد ضرايب رو بدست بيارم.مشكل من هم همينجاست .باتوجه به تعريف شما ا ز dll آيا يك چنين dll اي موجود هست كه مثل مطلب اين كار رو انجام بده؟؟
ممنون از جوابتون
Joined: 07 Dec 2005 Posts: 14 Location: iran- tehran
Posted: Tue Dec 13, 2005 10:20 pm Post subject:
سلام
من يك فايلي براي استفاده در ويژوال بيسيك براي ديجيتال فيلترها در لينك زير پيدا كردمhttp://www.mathfunctions.com/
ولي رايگان نيست و براي استفاده از اون بايد كارت اعتباري داشت اما من كه ندارم
نميدونيد چطوري ميشه ورژن آموزشي اون رو پيدا كنم؟
Chapters:
Foreword
About sampled sound
Adding two sine waves together
What's a filter?
Filter types: FIR and IIR
Interpolation of sampled sound
About filter design
Pole-zero IIR filter design
Some pole-zero IIR filters
Windowed FIR filter design
Filter implementation
Positive and negative frequencies
Frequency shifting
Nature of sound and music
Flanger
Wavetable synthesis
Bonus chapters:
Shuffling IIR equations
A collection of IIR filters
A collection of FIR filters
This is written for the audio digital signal processing enthusiasts
(as the title suggests ;) and others who need practical information on
the subject. If you don't have this as a "linear reading experience"
and encounter difficulties, check if there's something to help you out
in the previous chapters.
In filter frequency response plots, linear frequency and magnitude
scales are used.
Copy and use this text freely.
About sampled sound ---
Note that "sample" can mean (1) a sampled sound or (2) a samplepoint!
Sampled sound data is a pile of samples, amplitude values taken from
the actual sound wave. Sampling rate is the frequency of the "shots".
For example, if the frequency is 44100, 44100 samples have been taken
in one second.
Here's an example of sampling:
_0---0_
_/ --0__
0_ __0---0__ _0 -0---0___
\_ _-0- -0-- 0-__
+---0==-+--=0---+---+---+---+---+---+---+---+---+---+---+---0==-+---+
-0-- -0_
--0
<--->
1/Samplerate
The original sound is the curve, and "0"s are the sampled points. The
horizontal straight line is the zero level.
A sampled sound can only represent frequencies up to half the
samplerate. This is called the Nyquist frequency. An easy proof:
You need to have stored at least two samplepoints per wave cycle, the
top and the bottom of the wave to be able to reconstruct it later on:
If you try to include above Nyquist frequencies in your sampled sound,
all you get is extra distortion as they appear as lower frequencies.
------------------------------------ Adding two sine waves together ---
A Sound consists of frequency components. They all look exactly like
sine waves, but they have different frequencies, phases and amplitudes.
Let's look at a single frequency:
It gets silent. If we think of other cases, where the phase difference
is less than 180 degrees, we get sine waves that all have different
amplitudes and phases, but the same frequency.
Here's the way to calculate the phase and the amplitude of the
resulting sinewave... Convert the amplitude and phase into one complex
number, where angle is the phase, and absolute value the amplitude.
As you see, the phase of the new sine wave is 45 degrees and the
amplitude sqrt(1^2+1^2) = sqrt(2) = about 1.4
It is very important that you understand this, because in many cases,
it is more practical to present the amplitude and the phase of a
frequency as a complex number.
When adding two sampled sounds together, you may actually wipe out some
frequencies, those that had opposite phases and equal amplitudes. The
average amplitude of the resulting sound is (for independent originals)
sqrt(a^2+b^2) where a and b are the amplitudes of the original signals.
-------------------------------------------------- What's a filter? ---
The main use of a filter is to scale the amplitudes of the frequency
components in a sound. For example, a "lowpass filter" mutes all
frequency components above the "cutoff frequency", in other words,
multiplies the amplitudes by 0. It lets through all the frequencies
below the cutoff frequency unattenuated.
*** Magnitude ***
If you investigate the behaviour of a lowpass filter by driving various
sinewaves of different frequencies through it, and measure the
amplifications, you get the "magnitude frequency response". Here's a
plot of the magnitude frequency response curve of a lowpass filter:
Frequency is on the "-" axis and amplification on the "|" axis. As
you see, the amplification (= scaling) of the frequencies below the
cutoff frequency is 1. So, their amplitudes are not affected in any
way. But the amplitudes of frequencies above the cutoff frequency get
multiplied by zero so they vanish.
Filters never add any new frequency components to the sound. They can
only scale the amplitudes of already existing frequencies. For example,
if you have a completely quiet sample, you can't get any sound out of
it by filtering. Also, if you have a sine wave sample and filter it,
the result will still be the same sine wave, only maybe with different
amplitude and phase - no other frequencies can appear.
*** Phase ***
Professionals never get tired of reminding us how important it is not
to forget the phase. The frequency components in a sound have their
amplitudes and... phases. If we take a sine wave and a cosine wave,
we see that they look alike, but they have a phase difference of pi/2,
one fourth of a full cycle. Also, when you play them, they sound alike.
But, try wearing a headset and play the sinewave on the left channel
and the cosine wave on the right channel. Now you hear the difference!
Phase itself doesn't contain important information for us so it's not
heard, but the phase difference, of a frequency, between the two ears
can be used in estimating the position of the origin of the sound so
it's heard.
Filters have a magnitude frequency response, but they also have a
phase frequency response. Here's an example curve that could be from
a lowpass filter:
If you filter a sound, the values from the phase frequency response
are added to the phases of the frequencies of the original sound.
Linear (straight line) phase is the same thing as a plain delay,
although it may look wild in the plot if it goes around several times.
If your, for example, lowpass filter doesn't have a linear phase
frequency response, you can't turn it into a highpass filter by
simply subtracting its output from the original with equal delay.
*** Complex math with filters ***
The response of a filter for a single frequency can be expressed as a
complex number, where the angle is the phase response of the filter and
the absolute value the magnitude response. When you apply the filter to
a sound, you actually do a complex multiplication of all the frequency
components in the sound by the corresponding filter response values.
(Read chapter "Adding two sinewaves together" if you find this hard to
understand.) Example: The response of a filter is (0,1) at 1000Hz. You
filter a sine wave, with the phase & amplitude information presented as
the complex number (0,1), of the same frequency with it:
The phase of the sine wave got rotated 90 degrees. No change in the
amplitude.
*** Combining filters ***
Serial (A*B): In --> FILTER A --> FILTER B --> Out
The combined response of these two filters put in serial is the
response of A multiplied by the response of B (Complex numbers as
always!). If you only need to know the magnitude response, you could
as well multiply the absolute values.
Parallel (A+B): --> FILTER A -->
In Out
--> FILTER B -->
In the figure, both filters get their inputs from the same source.
Their outputs are then added back together, forming the final output.
Now you need to use addition in solving the combined response.
----------------------------------------- Filter types: FIR and IIR ---
FIR = finite impulse response
IIR = infinite impulse response
FIR filter is more straightforward, and easier to understand. Finite
impulse response means that when the filter input has remained zero
for a certain time, the filter output also becomes zero. An Infinite
impulse response filter never fully "settles down" after turning off
the input, but it does get quieter and quieter though.
*** FIR ***
A basic FIR filter could be:
ouput(t) = a0*input(t) + a1*input(t-1) + a2*input(t-2),
where "input" means the sample values fed to the filter.
In this case, people would speak of a "3 tap" filter.
It's up to the coefficients (a0, a1, a2) what this filter will do to
the sound. Choosing the coefficient values is the hardest part, and
we'll get to that later. To design your own filters, you need to
understand some of the math behind and know the right methods.
In the above filter example, only past input values are used. In
realtime filters, this is a requirement, because you don't know the
future inputs. In sample editors and such, you don't have this
limitation, because you have the whole input data ready when you begin.
If your filter is:
output(t) = a0*input(t+1) + a1*input(t) + a2*input(t-1),
and you need a realtime version of it, just convert it to:
output(t) = a0*input(t) + a1*input(t-1) + a2*input(t-2).
The only difference is the one sample delay in the realtime filter.
*** IIR ***
Unlike FIR filters, IIR filters also use their previous output values
in creating their present output. Here's a simple example:
This could be called "3 input, 3 output tap" filter.
IIR filters can never use future output values, because such don't yet
exist!
There can be several ways of implementing the same IIR filter. Some may
be faster than the usual input-output-and-coefficients way. Anyhow,
every IIR filter can be written in this form, and it must be used in
filter design and examining calculations.
An impulse response (= What the filter will do to a one samplepoint
impulse) of an IIR filter often looks more or less like this in the
sampledata:
Some badly designed IIR filters are unstable. This results in
ouput getting louder and louder instead of quieter and quieter. A
simple example of this is: output(t) = input(t) + 2*output(t-1). As
soon as it gets input data, it gets crazy.
*** FFT ***
The above described filter types process the data sample by sample.
FFT, Fast Fourier Transformation, doesn't. It does the work chunk by
chunk. A chunk, which must be of length 2^n, is first converted into
spectral information - complex numbers representing the phases and
amplitudes of the frequency components. In this form, it is very easy
to manipulate the spectrum. Then IFFT (Inverse FFT) is used to convert
the information back to a chunk of sampledata.
If you just take a chunk of sampledata, it has sharp edges, which is
bad for the FFT. Windowing functions are used to smoothen these edges.
"Raised cosine", cos(x pi/2)^2, is a well-known windowing function.
Here you see what happens when you apply a windowing function to a
chunk of sampledata:
Overlapping windows (= chunks) are used, so the whole sampledata goes
actually twice through the FFT. Here you see how the windows overlap:
______ _____________ _____________ ______________ ______________
_____________ _____________ _____________ _______________ ______
This is possible because of the symmetrical shape of the windowing
function. If you add together overlapping raised cosine windowing
functions, you get a flat result.
------------------------------------ Interpolation of sampled sound ---
Sometimes (resampling, precisely defined delay) you need to get
samplevalues from between the known samplepoints. That's when you need
interpolation. If you don't interpolate, and just throw away the
fractional part of your sampleoffset, you get a lot of high frequency
distortion:
In the example, the original samplepoints try to represent a sine wave.
The closer the interpolated curve is to a sine wave, the better the
interpolation algorithm is. The simpliest interpolation method is
linear interpolation. Straight lines are drawn between two adjacent
samplepoints:
Still looks quite "edgy" to be a sine wave. However, the improvement
to uninterpolated is significant. There's also a drawback - the
frequencies just below the Nyquist frequency get attenuated, even more
than without interpolation. Here's the formula for linear
interpolation: new = old(int)+(old(int+1)-old(int))*fract, where int
means the integer part of sample offset and fract the fractional part.
Next step could be Hermite curve, which gives in every way better
quality than linear interpolation:
___
0 _-0 0-_ 0
_ _
0_ _0 0_ _0
_ _ _ _
-0___0- -0___0-
With linear interpolation, you needed to know 2 samplepoints at time
to be able to draw the line. With Hermite curve, the number is 4. The
interpolation curve goes through the two middle points, and the points
1 and 4 are used in shaping the curve. The formula is a cubic:
new = a*fract^3 + b*fract^2 + c*fract + old(int+0), where:
A perfect interpolation also exists. By replacing all the sample points
with correctly scaled sinc curves, sin(pi x)/(pi x), and by adding them
together, you get exact, perfect interpolation. Here is one of the
samplepoints replaced with a scaled sinc curve:
Sinc curve is endlessly long, so you'd have to use all the samplepoints
in calculation of one interpolated value. A practical solution would be
to limit the number of samples to say 1000. It will still be too slow
for a realtime application, but it'll give great accuracy. If you
insist to use sinc in a realtime interpolation algorithm, try using a
windowing function and a low number (at least 6) of sinc curves.
*** Downsampling ***
If you want to downsample (decrease the samplerate), you must first
filter away the above Nyquist frequencies, or they will appear as
distortion in the downsampled sample.
----------------------------------------------- About filter design ---
In the process of filter design, you often need to make compromises.
To have sharp edges or steep slopes in the magnitude response, you will
need a big, and therefore slow filter. In other words, filters with low
number of taps practically always have gently sloping magnitude
responses.
In the case of IIR filters, sharp edges in magnitude often mean an
ugly (very nonlinear) phase frequency response, and close-to-linear
phase response a gently sloping magnitude response. With FIR filters,
an attempt to create very sharp edges may cause waving in the
magnitudes of nearby frequencies.
IIR filters are great for a realtime routine, because they are fast,
their properties (for example cutoff frequency) can be quickly changed
in the middle of action, and, they sound like real analog filters. :)
The nonlinear phase response of IIR filters usually doesn't matter.
FIR filters could be used where the quality and linear phase are
important, for example, in a sample editor. People who filter other
signals than sound, often desire linear phase frequency response.
With stereo signal, it is important to have identical phase changes on
left and right channels.
Some filters and their stylized magnitude frequency responses:
Lowpass:
__________________
|__________________
Highpass:
__________________
__________________|
Bandpass or peak:
_
_________________| |_________________
Notch, bandreject or bandstop:
_________________ _________________
|_|
Allpass:
_____________________________________ (Changes in phase only)
If you have a symbolic calculation program, i strongly recommend you to
use it in the mechanical calculations, just to make your life easier.
"Derive" is an old DOS program, but still very useful.
*** White noise ***
White noise means the sort of noise that has flat spectrum. You can
easily create it by using random numbers as samplevalues. If you want
to know the magnitude frequency response of a filter, apply it on a
long sample of white noise and then run a spectrum analysis on the
output. What you see is the magnitude frequency response of the filter.
Another way is to send a one-sample impulse, which originally has a
flat spectrum. An impulse looks like this in the sampledata:
0, 0, 0, 0, 1, 0, 0, 0, 0 - where the impulse is the "1" in the middle.
From the two, the impulse thingy is faster, but using white noise can
give cleaner-looking results, because errors will be less visible.
For much the same reasons, when you are watching videos, a still
picture will look more snowy than the running picture. Taking a
spectrum analysis on a long sample is usually done by dividing it to
smaller pieces, analyzing them separately and then taking the average
of all the analyses. My personal choice here would be the program
"Cool Edit 96", which is for Windows.
Pole-zero method is the easiest way of designing fast and simple IIR
filters. When you have learned it, you will be able to design filters
by yourself.
Here's the complex "Z-plane", the one used in the pole-zero method:
Imagine the frequencies to be wrapped around the unit circle. At angle
0 we have 0Hz, at pi/2 we have samplerate/4, at pi we have
samplerate/2, the Nyquist frequency. You shouldn't care about higher
frequencies, since they will never appear in the signal, but anyway, at
2pi (full cycle) we have the sampling frequency.
So if you used sampling frequency 44100 Hz, 0 Hz would be at (1,0),
11025 Hz at (0,1) and 22050 Hz at (-1,0).
What are poles and zeros then?
They are cute little things you can place on the Z-plane, like this:
__---|---__ o x = pole
_/ o | \_ o = zero
/ | x \
| | x |
-|-o------+--------|-
| | x |
\_ | x _/
\__o | __/
---|--- o
There are some rules you have to remember. Poles must always be inside
the unit circle, never outside or on it. Zeros can be put anywhere.
You can use any number of poles and zeros, but they must all have
"conjugate pairs", if they are not positioned on the "-" axis.
Conjugate pairs means that if you put for example a zero to
(0.6, 0.3), you must put another zero to the conjugate coordinate,
(0.6,-0.3). And the same thing with poles.
But hey! What do poles and zeros DO?
Poles amplify frequencies, zeros attenuate. The closer a pole is to
a frequency (on the unit circle, remember?), the more it gets
amplified. The closer a zero is to a frequency, the more it gets
attenuated. A zero on the unit circle completely mutes the frequency it
is "sitting on".
Now it could be the right time to try this out yourself. There are free
filter design programs around that allow you to play with poles and
zeros. One candidate could be: "QEDesign 1000 demo" for Windows. It's
somewhere on the Internet, you'll find it.
*** Designing a bandpass filter ***
The simpliest filter designed using pole-zero is the following
bandpass filter:
__---|---__ x = pole
_/ | \_ o = zero
/ | x \
| | |
-|--------+--------|-
| | |
\_ | x _/
\__ | __/
---|---
Poles amplify frequencies, so you could draw the conclusion that the
most amplified frequency is the one at the same angle as the pole.
And you are almost right! The only problem comes from the conjugate
pole, which also gives its own amplification. The effect is strongest
at angles close to 0 and pi, where the distance between the two poles
is the smallest. But don't let this confuse you, we'll get back to it
later.
So the angle of the pole determines the passband frequency. What's the
effect of the absolute value (= radius) then?
As stated, poles amplify frequencies, and the amplification is stronger
when the pole is closer to a frequency. In our bandpass filter,
increasing the radius of the pole causes the magnitude response to
become steeper and passband narrower, as you see here:
Let's call the radius "r" from now on. (Some of you might remember the
letter "q" from analog, "resonant" filters. This is much the same.) In
this case we have the limitation: 0 =< r < 1, since poles must be
inside the unit circle. So changing r changes steepness, resonance.
This "resonance" - it's not a magic thing, just one frequency being
amplified more than others.
*** From poles and zeros to filter coefficients ***
where z is frequency, in the (complex) wrapped-around-the-unit-circle
coordinate form. H(z) gives the response (complex!) of the filter at
the frequency z. P1, p2, p3 and so on are positions of poles and z1,
z2, z3 and so on positions of zeros. A0 is the first input coefficient
of the filter. Here's the IIR filter formula again, in case you have
forgotten:
Next we have to decide what to put to a0. This is called normalization.
The purpose of a0 is just to scale the output of the filter. In our
bandpass filter, we want the amplification at the passband frequency
to be 1. So we can write the equation:
We could compensate the effect of the conjugate pole by adding a zero
onto the "-" axis, between the poles. For example, if we had poles at
coordinates (0.6, 0.5) and (0.6,-0.5), we'd put a zero at (0.6, 0):
In case you want to use this filter, you should be able to do the
normalization yourself. I won't do it here.
*** Words of wisdom ***
It is easy to make a filter more efficient: Double all poles and
zeros. The frequency response of the new filter is the square of the
old. There are better ways, but this is the easiest.
If you put a zero on a pole, you neutralize both.
A pole outside the unit circle causes the filter to become unstable. A
pole on the unit circle may turn the filter into an oscillator.
Large number of poles and zeros means large number of taps.
Zeros affect the input coefficients, poles output.
Poles and zeros must have conjugate pairs, because otherwise you'd get
complex filter coefficients and, consequently, complex output signal.
With low r values, the most amplified frequency is not always at the
same angle with the pole, because of the effect of the conjugate pole.
Try differentiating the magnitude response if you want exact precision.
An IIR filter with no poles is a FIR filter.
--------------------------------------- Some pole-zero IIR filters ---
0 =< r < 1 always applies.
*** Bandpass with r ***
Read chapter "IIR filter design using pole-zero method".
*** Notch with r ***
__---|---__ A zero with a conjugate pair at:
_/ | o_ (cos(2pi f/SR), sin(2pi f/SR))
/ | x \
| | | A pole with a conjugate pair at:
-|--------+--------|- r * (cos(2pi f/SR), +- sin(2pi f/SR))
| | |
\_ | x _/ Use the frequencies at (1,0) and (-1,0) in
\__ | __o normalization. Depending on f, either of them
---|--- has higher amplification.
The higher the r, the narrower the stopband.
*** Lowpass with r ***
This can be done in several ways:
__---|---__ One or two zeros at: (-1,0)
_/ | \_
/ | x \ A pole with a conjugate pair at:
| | | r * (cos(2pi f/SR), sin(2pi f/SR))
-o--------+--------|-
| | | Use 0 Hz in normalization.
\_ | x _/
\__ | __/
---|---
__---|---__ A pole with a conjugate pair at:
_/ | \_ r * (cos(2pi f/SR), sin(2pi f/SR))
/ | \
| | x | Use 0 Hz in normalization.
-|--------+--------|-
| | x | Only works with f values below SR/4.
\_ | _/
\__ | __/ Same as the simple bandpass filter.
---|---
The higher the r, the stronger the resonation. Resonant lowpass filter
is surely the most used filter type in synthesizers.
*** Allpass with r ***
o __---|---__ A pole with a conjugate pair at:
_/ | \_ r * (cos(2pi f/SR), sin(2pi f/SR))
/ x | \
| | | A zero with a conjugate pair at:
-|--------+--------|- (1/r) * (cos(2pi f/SR), sin(2pi f/SR))
| | |
\_ x | _/ Allpass filter has a flat magnitude frequency
\__ | __/ response, but a wavy phase response. A zero
o ---|--- that has the same angle as a pole, but inverse
radius, neutralizes the effect of both in the
magnitude frequency response, but not in the phase frequency response.
The higher the r, the stronger the effect. One use for allpass filters
is to make the phase response of an IIR filter linear, by using a
correctly parameterized allpass filter in serial with the IIR filter.
*** Highpass with r ***
__---|---__ __---|---__ Almost the same as lowpass.
_/ | x \_ _/ | \_ Normalization frequency
/ | \ / | \ (-1,0), zeros, if any, at
| | | | x | | (1,0).
-|--------+--------o- -|--------+--------|-
| | | | x | |
\_ | _/ \_ | _/
\__ | x __/ \__ | __/
---|--- ---|---
---------------------------------------- Windowed FIR filter design ---
*** Impulse, sinc ***
If you read about sinc interpolation in the chapter "Interpolation of
sampled sound", you know that you can replace a single sample peak
(= impulse) in the sampledata with a correctly stretched sinc function.
Correctly stretched means amplitude*sinc(t).
When you run a spectrum analysis on an impulse, you get a flat spectrum
with upper limit at samplerate/2, the Nyquist frequency. Because
impulse = sinc, this is also the spectrum of sinc:
You could draw the conclusion that you get the sinc function if you sum
together all the frequencies from 0 to SR/2, and divide the sum by the
number of frequencies, to fulfil the equation sinc(0) = 1. And you'd be
right.
From the spectrum analysis, you know that all the frequencies summed
together have equal amplitudes. But what's their phase at the center of
the impulse? Sinc function is symmetrical around x=0, so is cosine - so
sinc must be made of cosines. If you test this with about 100 cosines,
you get a pretty close approximation of sinc.
The sum of all frequencies from 0 to 1 (comparable to SR/2), divided
with their number, can be written as: (Here "oo" means infinite)
n
___
\ i
> cos(x---) (x -> pi t)
/__ n
i = 0 sin(x) sin(pi t)
lim -------------- = ------ -> --------- = sinc(t)
n->oo n+1 x pi t
As done above, x must be replaced with "pi t", because the cycle length
of sin is 2 pi, which must be stretched to 2 (which is the "wavelength"
of the Nyquist frequency in the sampledata).
*** Phase shift ***
What if we replaced the cosines with sines? Lets try it! There's a
universal formula (which, btw, i invented myself) we can use:
n
___ x
\ i /
> f(x---) |
/__ n | f(x) dx
/
i = 0 0
lim ------------ = ----------
n->oo n+1 x
Therefore:
n
___ x
\ i /
> sin(x---) | (x -> pi t)
/__ n | sin(x) dx
/
i = 0 0 1 - cos(x) 1 - cos(pi t)
lim -------------- = ------------ = ---------- -> -------------
n->oo n+1 x x pi t
Now, if we replace all the impulses in the sound with this new
function, we actually perform a -90 degree phase shift! This can be
done by creating a FIR filter, where the coefficients are taken from
this new function: (1-cos(pi t))/(pi t), but in reverse order, by
replacing t with -t, so it becomes: (cos(pi t)-1)/(pi t).
Here's an example that explains why it is necessary to use -t instead
of t: Say you want to replace all the impulses in the signal with the
sequence 1,2,3. If the input signal is 0,1,0, common sense says it
should become 1,2,3. If you just use 1,2,3 as filter coefficients
in that order, the filtered signal becomes:
Which is not what you asked for! But if you use coefficients 3,2,1, you
get the right result, Ok, back to the -90 degree phase shift filter...
When you are picking the filter coefficients from (cos(pi t)-1)/(pi t),
at t=0 you unluckily get a division by zero. Avoid this by calculating
the limit t->0, on paper, or with a math proggy. If you use your brains
a little, you notice it is 0, because the filter formula is a sum of
sines, and sin(0)=0, so at t=0 it is a sum of zeros.
Like sinc, our new function has no ending, so a compromise must be made
in the number of taps. This causes waves in the magnitude response, and
attenuation of the very lowest and highest frequencies. By applying a
windowing function to the coefficients, you can get rid of the waves,
but i don't know anything that would help with the attenuation, except
more taps. The windowing functions used with FFT work here also. The
center of the windowing function must be at t=0, and it must be
stretched so that the edges are lay on the first and the last tap.
You can also get a phase shift of any angle "a":
n
___ x
\ i /
> cos(x---+a) |
/__ n | cos(x+a) dx
/
i = 0 0
lim ---------------- = --------------
n->oo n+1 x
(x -> -pi t)
sin(x+a) - sin(a) sin(-pi t + a) - sin(a)
= ----------------- -> -----------------------
x -pi t
Note that reversing t has already been done here, so we can take the
coefficients directly from this formula. The limit t->0 is naturally
cos(a), because all the cosines added together had phase "a" at x=0.
In case you didn't yet realize it, the main idea in FIR filter making
is to create a function that contains the frequencies you want to pass
the filtering. The amplitudes of the frequencies in the function
directly define the magnitude frequency response of the filter.
The phases of the frequencies define the phase response.
Reversing the coefficients is only necessary with phase shifting
filters, because filters that do not introduce a phase shift of any
kind are symmetrical around t=0.
*** Defining the frequency range included ***
If you use sinc as your filter coefficient formula, you actually do no
filtering, because all the frequencies from 0 to Nyquist are equally
presented in sinc. Here you'll see how you can select which frequencies
will be present in your filter coefficient formula. Remember where we
originally got sinc from:
x
/
| (x -> pi t)
| cos(x) dx
/
0 sin(x) sin(pi t)
------------ = ------ -> ---------
x x pi t
In the integral, the upper limit (1x) actually represents the highest
frequency included (1), and the lower limit (0x) the lowest (0). So if
you want a formula for a bandpass filter, you can write:
top x
/
|
| cos(x) dx
/
bottom x
------------
x
where top and bottom are the cutoff frequencies in such way that 1
means the Nyquist frequency, and 0 means 0Hz. Now just put there
whatever frequencies you want, calculate, and replace x with (pi t).
There's your filter coefficient formula ready! For example, if you
want to make a halfband lowpass filter (which naturally has cutoff
frequency at samplerate/4, same as Nyquist frequency / 2):
0.5 x 0.5 x
/ /
| | (x -> pi t)
| cos(x) dx | cos(x) dx
/ /
0 x 0 sin(x/2) sin(pi t/2)
------------ = ------------ = -------- -> -----------
x x x pi t
To create multi-band filters, you can combine several bandpass filter
formulas by adding them together.
*** The equalizer example ***
If you want to make an equalizer (a filter that allows you to define
the magnitudes for certain frequencies), you probably sum together a
lot of bandpass filter formulas, scaled by the magnitudes you want for
the frequency segments. This gives you a magnitude response that looks
very much like as if it was made of bricks:
(*=defined frequency)
There are three ways. The first way is to "use smaller bricks", meaning
that you divide the frequency into narrower-than-before segments and
use interpolation to get the magnitude values for the new narrow
bandpass filters you then combine.
The second way is to define a polynomial (like ax^3+bx^2+cx+d) that has
the wanted characteristics (and where x=1 represents freq=SR/2), and to
make the magnitude response of your filter to follow it. This is
possible.
The third way is to add together several bandpass "ramp" filter
formulas. In the magnitude response this solution looks like straight
lines drawn between the adjacent defined frequencies. This is also
possible, and, in my opinion, the best solution.
*** Polynomial shaped magnitude frequency response ***
In sinc, all the cosine waves added together have equal amplitudes, as
you see here - all the frequencies are treated equally:
n
___
\ i
> cos(x---) (x -> pi t)
/__ n
i = 0 sin(x) sin(pi t)
lim -------------- = ------ -> --------- = sinc(t)
n->oo n+1 x pi t
You can change this by putting there a function g() that defines the
amplitudes of the cosine waves of different frequencies:
n
___
\ i i
> cos(x---) * g(---)
/__ n n
i = 0
lim -----------------------
n->oo n+1
If the function g(x) is form a*x^b, the calculations go like this:
n
___ x
\ i i /
> cos(x---) * a*(---)^b |
/__ n n | cos(x)*ax^b dx
/
i = 0 0
lim -------------------------- = -----------------
n->oo n+1 x^(1+b)
For a simple example, if we want the magnitude frequency response to be
a straight line, starting from 0 at 0Hz, and ending at 1 at SR/2, we
define g(x) = x:
y
To make a bandpass ramp, you must first define the polynomial g(x) that
describes how the magnitude behaves inside the bandpass limits. The
magnitude is linear inside the limits, so the polynomial g(x) must be
form c*x+d. C and d can be solved from the equations:
/ g(x1) = y1
<
\ g(x2) = y2
where x1 is the lower frequency limit, and x2 the higher. Y1 and y2 are
the magnitudes of the limit frequencies. Remember that here x=1 equals
frequency=SR/2. OK, here are c and d solved:
G(x)=c*x+d is a polynomial, and you already know how to make the
magnitude frequency response have the same shape (Section "Polynomial
shaped magnitude frequency response") as a polynomial. You also already
know how to include only limited range of frequencies (Section
"Defining the frequency range included") in your coefficient formula.
Combine this knowledge, and you can write the coefficient formula for
the ramp bandpass filter:
x2 x x2 x
/ /
| |
| cos(x)*c*x^1 dx | cos(x)*d*x^0 dx
/ /
x1 x x1 x
-------------------- + ----------------- =
x^(1+1) x^(1+0)
cos(x2*x)+x2*x*sin(x2*x)-cos(x1*x)-x1*x*sin(x1*x)
c ------------------------------------------------- +
x^2
sin(x2*x)-sin(x1*x)
d ------------------- (And then, as always, x -> pi t)
x
A note about implementing the equalizer... If the equalizer is to be
adjustable realtime, recalculating the whole equalizer filter formula
with all the trigonometric functions may turn out too heavy. It may be
better to precalculate coefficients for several overlapping filters,
for example these for a three-channel equalizer:
Last edited by unknown on Fri Dec 16, 2005 8:59 pm; edited 1 time in total
خيلي واضح جواب داديد مرسي
ديجيتال فيلترها در واقع فيلترهاي نرم افزاريند كه يا در ميكرو كنترلرها يا در pc قابل پياده سازيند
در واقع سيگنال ارسالي به ميكرو از طريق A/D يا ارسالي به كامپيوتر از طريق پورت سريال پس از ورود با توجه به تعريف برنامه طراح فيلتر و بازيابي ميشوند
در واقع با استفاده از تابع ضرايب تابع تبديل و تبديل z اين فيلتر عمل ميكنه دقتش هم به تعداد نمونه هايي هست كه ميگيريم
مثلا يك سينوسي رو با ديجيتا ل كردن بصورت نقطه هايي با دامنه هاي مشخص تبديل بعد با توجه به فركانسمون ضرايب رو بدست مياريم اين نقطه ها رو با يك سري عمليات محاسباتي (با توجه به اون ضرايب)تغيير و دوباره با يك D/A بصورت آنالوگ فيلتر شده در مياريم. در واقع مزيت آن هزينه كم اون در تعداد انبوه و اينكه اگر تغييري در طراحي بوجود آمد ديگر نيازي به تغيير سخت افزار نيست و فقط پروگرام كردن مجدد آن نياز ميشه.
توي مطلب به راحتي با دو دستور قابل اجراست .ولي با vb بايد ضرايب رو بدست بيارم.مشكل من هم همينجاست .باتوجه به تعريف شما ا ز dll آيا يك چنين dll اي موجود هست كه مثل مطلب اين كار رو انجام بده؟؟
ممنون از جوابتون
يک راه ساده استفاده از توابع کتابخانه اي Matlab در محيط برنامه نويسيه. البته براي استفاده در vb مشکل اصلي تبديل header file ها است که در matlab براي c++ طراحي شده اند. ولي اگر از محيطهاي C استفاده کني عاليه. من خودم تو Builder 6 و Excel استفاده کردم خيلي راحته.
راه دوم اينه که در خود Matlab برنامه بنويسي و بعد کامپايلش کني تا به صورت standalone بيرون از Matlab به تنهايي کار کنه.
ولي براي vb يک راه ديگه هم وجود داره. ولي خودم هنوز تستش نکردم. اگه درست کار کرد براتون ميفرستم.
Joined: 07 Dec 2005 Posts: 14 Location: iran- tehran
Posted: Sun Jan 01, 2006 12:07 am Post subject:
سلام
منظورتون اينه كه در يه mfile با دستورات طراحي فيلتر كار كنم بعد اون رو run كنم.اينكه فايل standalone بوجود بيارم به چه صورتيه؟؟لطفا بيشتر توضيح بديد
ممنون
Joined: 07 Dec 2005 Posts: 14 Location: iran- tehran
Posted: Sun Jan 01, 2006 12:22 am Post subject:
سلام
ببينيد من 8 تا فيلتر ديجيتال ميخوام 4 تا بالا گذر 4 تا پايين گذر
كه باتوجه به انتخاب كاربر به تابع مربوطه مراجعه و سيگنال فيلتر ميشه
من از اين فيترها اطلاعات فركانس قطعشون رو دارم
لطفا هر اطلاعاتي كه به من كمك كنه رو برام ميل كنيد
ممنونم
zx_moghimi@yahoo.com
سلام
منظورتون اينه كه در يه mfile با دستورات طراحي فيلتر كار كنم بعد اون رو run كنم.اينكه فايل standalone بوجود بيارم به چه صورتيه؟؟لطفا بيشتر توضيح بديد
ممنون
نه، منظورم اينه که کتابخانه matlab را در محيطي مثل ++vc وارد کني. اونوقت ميتوني از متغيرهاي matlab و همينطور توابع پردازش سيگنالش مثل توابع فيلتر در محيط ويژوال استفاده کني.
روش دوم هم اينه که در matlab برنامه فيلتر رو به صورت mfile بنويسي، ولي بجاي اينکه اونو تو خود matlab اجرا کني، تو خود matlab کامپايلش کني تا يک فايل exe بهت بده. بعد اونو مستقيم تو ويندوز اجرا کني.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum