Things About Convolution and Filters
Derived from Real Sound Synthesis Labs, and 
CCRMA Summer DSP Course 2004-2008
DSP Lab 2:  Filters and Convolution
by Perry Cook, 1998-2009

If you're reading this, hopefully you've been able to 
get everything copied and the files compiled.

FIR FILTERING ---------------------------------------------
Get a shell, and type:

cd Filter09 (note you might need to add extra path stuff)

>  filter 2ptavg.spc homer8.wav homeravg2.wav
>  filter 2ptavg.spc homeravg2.wav homeravg4.wav
>  sndplay homer8.wav
>  sndplay homeravg2.wav
>  sndplay homeravg4.wav

Open these in snd or MATLAB and compare their spectra.

Now type

>  filter 8ptavg.spc homer8.wav homeravg8.wav

Open homeravg8.wav in snd or MATLAB and compare spectra and
sound to homer8.wav and homeravg2.wav

Turns out the effect is not huge, since it does most of the
work way up at 1/2 sample rate.  But continued reapplication 
of the filter will make the effect more profound.  This will
be important when we get to the Plucked String model.

CONVOLUTION ------------------------------------------

type 

>  convdir homer8.wav CCRMA8.wav homerCC.wav

listen, look, enjoy.

type 

>  convdir homer8.wav homer8.wav homersquared.wav

what's up with that??

try convolving CCRMA8 with CCRMA8.

Get what you expected?

Find two other very short files and convolve them.
They must be mono, 16 bit, any sample rate (but
the sample rates should be the same for predictible
results, not that predictibility is desirable).Check
this using sndinfo in the shell.

FAST  CONVOLUTION ------------------------------------------

in your Filter1 directory, type 

>  convfht homer8.wav CCRMA8.wav homerCC.wav

notice anything?  Like it's LOTS faster?  What a wonderful
hack is the Fast Fourier Transform!!

listen, look, enjoy.

type 
>  convfht homer8.wav homer8.wav homersquared.wav

now type:
>  reverse homer8.wav homerrev.wav
>  convfht homer8.wav homerrev.wav homerrevsquared.wav

listen to and and look at homersquared vs. homerrevsquared.  
What's this reversed business?  What is it good for?

try convolving CCRMA8 with CCRMA8 backward.

This is called autocorrelation, defined by:

y(n) = Sum_k[ x(k) x(n+k) ]

like convolution, but different.

cross correlation is defined as:

y(n) = Sum_k[ x(k) h(n+k) ]

equiv. to convolution of x and a time-reversed h.

Now, clip out the individual vowels (the "ehh" from the
word "test," the "ooo" from the word "two", etc.) from 
the speech8.wav file and save them as new files in 
the Filter1 directory.  Or you can use the baby vowels
stored in the Sounds directory.

Make reversed versions of them.  Then form the autocorrelations
by convolving the files with their reversed versions.  
For example:  (NOTE: here "ehh" is whatever you named your file).

>  reverse ehh.wav ehhrev.wav
>  convfht ehh.wav ehhrev.wav ehhcorr.wav

listen to these, but more importantly look at them in the
time domain using snd.  We'll talk about what this all means.

AMDF --------------------------------------------------

AMDF[x(n)] = Sum_k[ abs(x(k) - x(n+k)) ]

where abs() is absolute value.

Process your edited vowels using the amdf program.  Note
that amdf only takes one input argument, and one output
argument.

>  amdf ehh.wav ehhamdf.wav

look at these in snd.  Compare these to the auto-correlated
versions.  That is, line up ehhcorr.wav and ehhamdf.wav and
compare them.  Compare their spectra.  Is there anything
wrong (not morally, just mathematically) with looking at 
the spectrum of amdf?

Autocorrelation-based pitch detection ---------------------

try running the pitch detector on some files.  Note that it
creates a new wave file (psynth.wav) which is a pulse wave 
synthesis of the raw detected pitch from the analyzed file.  
How well does it work?  Does it work better for some files 
than others?

IIR FILTERING ---------------------------------------------

>  filter biQuad.spc speech8.wav speechRinging.wav
>  sndplay speech8.wav
>  sndplay speechRinging.wav

Open both of these in snd, listen, and compare their spectra.
Cool huh??  Sounds just like a really old telephone, doesn't
it?  DSP rocks!
------------------------------------------------------

Clean up (delete) any spurious soundfiles you made, by using the 
browser or rm in the shell.  You can always regenerate 
them by executing the programs again.
______________________________________________________

ChucK!!!
cd to the ResoLabChucK directory and view the README.txt
therein.  Try this command first:

wish Resolab1.tcl | chuck resolab1.ck

If that works, then you can try one of the GUI scripts.

______________________________________________________

Filter/Convolution Lab Puzzlers/Tinglers/Homeworks/Whatevers

For composers:  Make a short piece using convolved/correlated
files as material.  Don't try to convolve/correlate huge files,
you'll crash the machine and make the system administrators
not like you.

For C programmers:  Make new versions of convfht.c to
do cross-correlation and auto-correlation.  Maybe do it
as command line switches, or create new stand-alone versions.

For math-heads: Analyze the 8-point moving average FIR
filter: y(n) = 0.125 * (x(n) + x(n-1) + ... + x(n-7)).
Replace delays with Z^-N, substitute cos(w) - jsin(w)
for Z^-1, multiply out the real terms and imaginary terms,
and solve.  (Note, this is really a lot of work for 
nothing, so I'd suggest you try 3rd order, then give up.)

For computer scientists: Look at the Fast Hartley transform
used in convfht.c.  It's a radix-4 divide/conquer factorization
of the Discrete Hartley Transform.  Discuss.  The kernal for
the Hartley transform is cas(w) = cos(w) + sin(w).  How is this
different from the Fourier kernal?  Implications?


