You are on page 1of 38

Fourier & Filters

Time vs. Frequency

• in order to analyze and process waves (sound


or light) we need to be able to examine them
as a series of samples (time domain) or as a
collection of frequencies (frequency domain)
• when you examine sound in the time domain
you can easily adjust the amplitude (just
adjust the scale of each sample), mix several
sounds together (add samples), delay a sound,
and even cut out segments and paste in others
Frequency Domain

• in the frequency domain you can easily


isolate specific components such as treble
and bass and manipulate them separately,
excellent for compression (MPEG uses
this), sound manipulation (removal of hiss,
pops and clicks etc. via filtering) and
compare two waves for similarity (useful
for voice recognition)
Fourier

• to convert a signal from time domain to


frequency domain you use a Fourier
Transformation
• two version of Fourier, forward (time to
frequency) and inverse (frequency to time)
• forward and inverse transformations are exact
(except for some round-off error) meaning if
you convert a signal into frequency domain
then convert back you get the same signal
Limitations

• real signals have the frequency spectrum


vary from instant to instant while Fourier
requires a fixed segment of time
• Fourier transformations also assume
(wrongly) that the fixed segment of the
signal repeats
• sample sizes are small, usually 32 to 1,024
samples
• fewer samples give you less frequency
Version of Fourier
• there are more algorithms for Fourier transformation
(all focused on improving performance) than
flavours of ice-cream
• there are also slightly different techniques for
different situations. These include:
– original Fourier Transformation - used for
analog signals
– Discrete Fourier Transformation (DFT) - as
above but used for digital signals
– Discrete Cosine Transformation (DCT) - used
Sine Waves

• frequency = number of complete cycles per


second
• duration or period = time to complete ONE
cycle
• f=1/T where T is the period
• height of a wave on a graph is the amplitude
(digital value, electrical voltage, or air
pressure) see fig. 15
• use the sin() or cos() function
Sin()

• sin(t) has an amplitude of 1 (value in front


of sin() indicates amplitude)
• sin(t) has a duration of 2 seconds giving a
frequency of 1/(2) cycles per second
• Asin(2ft) is a sine wave with amplitude A
and frequency f (where t is time in seconds
and f is the frequency value in Hz)
Digital use of sin()

• in sampled signals we let t represent the


sample number
• Asin(2ft) then is a sine wave with
amplitude A and frequency f*S where S is
the sampling rate
• for Fourier we only work with a group of
samples of size N thus Asin(2tf/N) is a
wave with an amplitude A and a frequency
of f*S/N
Phase

• time delays offset one sine wave from


another (see fig. 16)
• can be measured as an offset in time or
(more usually) as a fractional difference in a
cycle (called phase)
• phase is measured in degrees (or radians)
see fig. 17
An example

1,000 Hz signal requires 1/1,000 second for


one cycle.
Delay this by 1/2,000 second (half cycle)
Get a 180 degree phase shift.
Delay a 250 Hz signal by 1/2,000 second
Have a 45 degree phase shift
Adding waves

• if you add two sine waves with the same


frequency the result is a new sine wave with
the same frequency
• this remains true even if you add two sine
waves with different amplitudes and phases
• e.g. Asin(2ft) and Bcos(2ft) are two sine
waves with different amplitudes and different
phases but the same frequency
• cosine is just the sine wave sifted by 180
Frequency Extraction

• question: how loud is a 1,000 Hz sine wave


in a given sound wave?
• DFT can extract a single frequency from a
wave and by repeating it for different
frequencies you can build the spectrum of
the sound
Multiplying waves

• depending on the frequency of the waves


you get different results
• specifically if you multiply two sinewaves
that have the same frequency and phase the
product is non-zero
• if you multiply two sine waves that have
different frequencies, the product is zero
Example

• if you have a signal and you want to know how


loud a 1,000 Hz frequency is in the signal,
multiply the the signal by the 1,000Hz
frequency (see fig. 18)
• the final sum will be proportional to the
amplitude of the 1,000 Hz frequency found in
the signal (we are currently assuming the phase
is the same)
• if the 1,000 Hz frequency is NOT in the signal
the sum will be 0 (see fig. 19)
Extracting a single frequency
from a Digital Signal

• to measure the amplitude of a single


frequency, you multiply your signal by a
sine wave of that frequency and add
together the resulting samples
amplitude = stcos(2tf/N) over t=0 to N-1
range
• where s0, s1, …, st, … represent the
samples, t represents the sample number
and f is related to the frequency desired
Half of Fourier

• to get frequencies from a digital signal then


the formula is:
• Af = stcos(2tf/N) where t=0 to N-1 (N
being number of samples in a group), Af
represents the amplitude (it’s only
proportional not equal to amplitude) of the
frequency f*S/N, t reprsents the sample
number
Complex Numbers

• in order to complete the picture of Fourier


the phase needs to be accounted for, for this
Complex numbers are used
• complex numbers are two dimensional
values, one part specifies the Real portion,
the other the Complex portion thus C
(Real is a subset of Complex)
• the two portions are distinguish with the use
of i where i=-1 (read as squareroot of -1)
Complex Examples

• 5+3i contains a real portion of 5 and a


complex portion of 3
• 8 contains a real portion of 8 and a complex
portion of 0 and can be written as 8+0i
• you can graph complex numbers using a
real and complex axis and plot the value
like a vector. The length of the vector is
then the squareroot of the sum of each
portion squared (Pythagorus)
More on Complex

• you also can find the angle of the complex


vector with tan-1(a/b) where a is the real
portion and b is the complex portion
• to summarize
magnitude = a2+b2
angle = tan-1(a/b)
Math with Complex Numbers

1. Addition
(a+bi)+(c+di) = a+bi+c+di=(a+c) + (b+d)i
2. Subtraction
(a+bi)-(c+di) = a+bi-c-di = (a-c) + (b-d)i
More Math

3. Multiplication
(a+bi)(c+di) = ac+adi+bci+bdi2 = (ac-
bd)+(ad+bc)i
4. Division
(a+bi)/(c+di) = (a+bi)(c-di)/((c+di)(c-di))=???
Accounting for Phase

• as indicated, the formula given for Fourier


is only half the picture since we must
account of the phase of the frequencies
• to get the phase you just need to measure
two amplitudes each corresponding to a
different phase
• one phase is represented by cos() and the
other by sin(). We use complex numbers to
perform both calculations at once
Fourier

Af = (stcos(2tf/N) -istsin(2tf/N)) where


t=0 to N-1
• Af is now a complex number and the two portions
of the complex number give the amplitude of two
sine waves at different phases
• to get the amplitude of the frequency use the
formula for the magnitude of complex numbers
• to get the phase use the formula for the angle of
complex numbers
Inverse Fourier

• the inverse is similar


st = (Afcos(2tf/N) +iAfsin(2tf/N)) where
f=0 to N-1
• only the real portion is used as the final
signal values (note: since Af is complex it is
not enough to only calculate the cos()
portion - MAKE SURE YOU
UNDERSTAND WHY!)
A different look for Fourier

• there is another more compact look to


Fourier and that is using the “e” notation
ez = ex+iy = exeiy = ex(cosy + isiny) where
z=x+iy giving
Af = ste-2itf/N, where t=0 to N-1
st = Afe2itf/N, wheref=0 to N-1
Scaling

• the problem with using the above formulas


for Descrete Fourier and Inverse Descrete
Fourier is you won’t get the exact samples
you started with, the result will be N times
too large
• to compensate you must introduce a factor
of 1/N somewhere and there are 3 ways to
do this
Scaling continued
• Multiply the Forward DFT by 1/N
Af =1/N* (stcos(2tf/N) -istsin(2tf/N)) where t=0 to N-1
st = (Afcos(2tf/N) +iAfsin(2tf/N)) where f=0 to N-1
• Multiply the Forward and Inverse DFT by 1/N
Af =1/ N * (stcos(2tf/N) -istsin(2tf/N)) where t=0 to N-1
st = 1/ N * (Afcos(2tf/N) +iAfsin(2tf/N)) where f=0 to
N-1
• Multiply the Inverse DFT by 1/N
Af =(stcos(2tf/N) -istsin(2tf/N)) where t=0 to N-1
st = 1/N* (Afcos(2tf/N) +iAfsin(2tf/N)) where f=0 to N-1
Some DFT factors
• Fourier Transforms assume a periodic (repeating)
signal (for the portion or group of samples you
apply it to)
• this results in some incorrect frequency spectrums
if the start end of samples of the group do not flow
(no sudden jumps) see fig. 20 and 21, 22
• to deal with this problem a technique called
windowing can be used to scale down the start and
end of the samples from the group to reduce sharp
jumps that obscured the spectrum we want (see
Groups of samples

• unless you have a very fast machine and a


lot of time (years) to wait for your DFT to
complete processing you need to break your
digital samples into small groups of size N
(whatever number you like, large groups
will take more time but give a more
complete frequency spectrum)
• now you send these N samples off to your
DFT function, then you send the next N
An example

e.g. if you use 512 sample size and the


sampling rate is 11,025 samples/sec you
will have approx. 21.5 groups per second.
Your spectrum will use the following
frequencies (approx. values here): 0, 21.5,
43, 64.5, 86, … Hz up to 11,003.4Hz
• note: the 0 frequency is often referred to as
the DC component because it is the sum of
the samples (no frequency applied) WHY??
Filtering

• used to modify the frequency content of a


sound
• treble and bass controls are filters, filter can
be used to isolate a person’s voice by
allowing only certain frequencies (voice
recognition systems), remove hiss from old
recordings, etc.
Adding sine waves
• one way to change the phase of a signal is to delay
it
• if you add a 500Hz sine wave to itself but delayed
by 1/1,000 second you get no wave (see fig. 24)
• if you keep the same delay but use a 1,000 Hz sine
wave the amplitude is doubled
• thus using a system to add signals where part is
delayed will modify the wave depending upon
their frequency (see fig. 25)
Comb filters

• this filtering system is known as a comb filter


because of what the filter does to the amplitude
of a sine wave (see fig. 26)
• in this case multiples of 1,000 Hz are doubled
and other frequencies are suppressed
• the idea is that if sin(t) is one wave then
sin(t+p) is the other where p represents the
amount shifted. Using trig. you get the sum as
2cos(p/2)sin(t+p/2). Since p is fixed the
amplitude is 2cos(p/2)
FIR filters

• by using a more complex set of delays you


can selectively reinforce or suppress any
frequency
• if you put an impulse into the Comb filter
you will get two copies of the same
impulse, separated by 1/1,000 second. Since
this output terminates this filter is called a
finite impulse response (FIR) filter
Designing a simple FIR filter
• an ideal low-pass filter has a cut-off frequency
meaning it blocks frequencies above that point and
passes lower frequencies unaltered (see fig. 27)
• the idea is that if we have a series of numbers that
represent the amplitude of a different frequency
we just multiply each high-frequency by 0 and
each low-frequency by 1
• thus by using DFT to convert samples into
collection of frequencies and then modify the
components then use inverse DFT to convert back
Convolution

• an alternative method is to use Fourier Transform to


convert multiplication in the frequency domain into
another operation in the time domain (known as
convolution)
• this filter picks out certain samples and multiplies
them by weighted values then sums them to get a
single output
• convolution is the pattern of weighting and
summing (see fig. 28)
Filter Design

• start with the desired frequency response and


figure out the weights to use in your
convolution
• to figure out the weights just apply the Inverse
DFT to the frequency response (see fig. 29 and
30)
• use just the real portion from the inverse DFT
• the more samples in the filter the closer it will
be to an ideal filter (see fig. 31)

You might also like