Dec 252017
 
Turns Soundblaster into a spectrum analyser.
File MICFFT11.ZIP from The Programmer’s Corner in
Category Science and Education
Turns Soundblaster into a spectrum analyser.
File Name File Size Zip Size Zip Type
MICFFT.EXE 57098 34758 deflated
MICFFT11.TXT 31121 10810 deflated

Download File MICFFT11.ZIP Here

Contents of the MICFFT11.TXT file


MicFFT v1.1
March 31, 1993

Written by Craig M. Walsh
CIS 70701,1614
Internet: [email protected]
[email protected]


Introduction:

MicFFT is a program that samples data from a sound card or
addressable analog-to-digital (ADC) port, and plots out the
spectral features of the sampled waveform. The program utilizes
a Fast Fourier Transform (FFT) algorithm to transform the
sampled data (in the time domain) to the frequency domain. The
program gets 256 Pulse Code Modulated (PCM-see below) samples
from the ADC at a defined sampling rate, and then plots the
transformed data. MicFFT continuously loops between sampling
and plotting. Thus, the program acts somewhat like a realtime
spectrum analyzer, and on fast machines with fast sound cards,
is capable of impressive performance. MicFFT can be likened to
spectrum analyzers found on some audio equalizers, and can
provide detailed information on the spectral components and
relative intensities of continuous waveforms.


Improvements in version 1.1:

Since many of you heeded my call for comments in the last
version, I was able to gain insight into what improvements to
include in this version of MicFFT. The main problem I found was
that the original version of MicFFT did not support Sound
Blaster or Sound Blaster-compatible cards, even though I
presumed it might. After some investigation, I found that
sampling on the SB card requires quite a bit of additional code
that was not included in the original version of MicFFT. Since
I don't have an SB card (I have a Covox Sound Master II, and am
quite happy with it so far!), I had to find someone out there
with one to help out. Fortunately, Chris Pye decided to join
the MicFFT "development team", and has supplied the SB sampling
routines (along with additional ideas, etc.) for this new
version. Now, MicFFT v1.1 has explicit support for SB 8-bit
cards (16 bit sampling is, sadly, not included yet).
Additionally, other cards that have Sound Blaster compatibility
should also work with MicFFT (see Compatibility); I hope this
time I don't eat my words!

MicFFT also has a new function that provides for freezing
waveform sampling and allows detailed analysis of the frozen
waveform. This function has a positionable cursor-line, which
can be used to determine the frequency and amplitude of a
specific band on the plot. The program also has a new plotting
mode that allows plots to be generated in either bar graph
style, as implemented in the original version, or line graph
style, which produces output that may be more suitable to
certain applications. Output is also scaled in decibel (dB)
steps. Some new filtering options have been added to MicFFT
v1.1; Squelch allows filtering of spurious noise below a set
threshold; Cosine envelope is used to reduce transformation
errors resulting from sampling discontinuities (see Cosine
Envelope, below). In addition, certain bugs that were not
identified before the original release have been fixed (I hope).


Requirements:

MicFFT is a computing-intensive program and requires a fast
computer for achieving rapid output. It was developed on a
33MHz 486-DX (with floating-point coprocessor), with SVGA, a
Covox SoundMaster II sound card. Graphics modes tested include
both EGA and VGA, so both should work (see Options). MicFFT
will look for a coprocessor (and will run much faster if one is
found), but will emulate one if a coprocessor is found.
Additionally, some 286-specific code is included for speed, and
thus it will not work properly on machines with lesser
processors. Since the sampling routine is timed and independent

of the transformation and plotting, the speed of a particular
machine should not affect the accuracy of frequency output; only
the rate at which data is plotted will be affected.


Supported Sound Cards:

As stated above, this version of MicFFT explicitly supports the
Covox SoundMaster II (default) and the Sound Blaster cards for
8-bit sampling. The Sound Blaster mode, which includes
additional code for sending commands to control the sampling
specifics, is available using the -B1 flag (see Options). Other
cards may work as well, particularly if some means for Sound
Blaster compatibility is provided by the card. For example, the
Pro Audio Spectrum card may work if a Sound Blaster
compatability driver for that card is loaded before running
MicFFT. This has not been tested yet, but you should check out
your sound card documentation for specifics. Covox
compatibility (default) is likely not explicitly provided by
other sound card vendors. However, it may be generally useful
to those whose cards are capable of polling the ADC
independently (without explicit commands for controlling
sampling); MicFFT provides a means for changing the default ADC
port address. In the Covox mode, MicFFT simply looks for data
at this port at a defined rate using timing routines in the
program. The default port address included in the program is
22F (hex). This is actually a port address (0x220h) and ADC
offset (0x00F) added together. If you have a card where a port
address is given in the documentation, and an ADC input offset
is also given, just add the two together (using a hex
calculator) and use the -P flag to let MicFFT know which address
you need to use. I realize that this discussion may be a bit
technical, but I provide it so that users with cards other than
the above mentioned can attempt to figure out which options to
alter. I know that incompatibility is frustrating, so if you
have a card which is not explicitly supported (and there is
really a growing list), I will include compatibility for such
cards for those who can get me specific code for PCM sampling.
I prefer to have short code segments (in C, or assembler if
possible) to look at rather than libraries, since I need to be
able to control some of the aspects of sampling, and I need you
to volunteer for testing the modified code. Contact me at the
address provided (see Contacting the Author), and I will happily
discuss your specific needs. (It should be noted that I may
decide to port MicFFT to Windows, if I can pull together some
cash for buying a Windows C compiler. This would be helpful,
since compatibility would not be an issue here.)


Lesson 1. PCM sampling:

Sampling is a process whereby a continuous stream of data is
broken up into chunks of data usable by the sampling system.
PCM sampling is a process whereby this continuous data is polled
at a defined rate. Since we are interested in analog signals
most of the time (the type that enter our sound cards via the
ADC port), it is necessary to first transform the data from an
analog representation to a digital one. This is the job of the
ADC. This process is fairly complicated, but suffice it to say
that an analog value (from - infinity to + infinity) is
transformed to an explicit digital value. Since the digital
value can represent only a discrete value, there may be some
loss of resolution in this transformation. 16 bit cards (which
can represent 64K different input levels) are much more accurate
for this purpose than 8 bit cards (which only can reproduce 256
different levels). Also, since PCM-sampling assumes that the
analog waveform has components on either side of zero (i.e.
negative and positive parts of a wave), one half of the digital
range is negative, the other half positive. This reduces the
number of possible digital absolute value amplitudes by a factor
of two, which affects the dynamic range (i.e. the lowest to
highest amplitude) of a particular digital system.

Digital conversion produces a sample value, and this is done at
a discrete sampling frequency so that points of a continuous
analog waveform are approximated by a set of digital values.
These digital values can be stored into the computer's memory
and played back to reproduce an approximation of the original
analog waveform. The process of rebuilding an analog waveform
from digital samples requires that the samples are "played back"
at a rate similar to the rate at which the samples were
originally obtained. To do this, each sample is transformed
from a digital value to an analog value (using a digital-to-
analog converter, or DAC) and this is done for all of the stored
samples at the chosen sampling frequency. Thus, an
approximation of the original analog waveform can be reproduced.

The accuracy of sampling thus depends on two major variables:
the sampling rate and the number of possible sample values. The
sampling rate can affect accuracy since it defines the highest
possible frequency from the original waveform that can be
sampled. The Nyquist sampling theorem dictates that the
sampling rate must be at least twice the frequency of the
highest frequency component to be sampled. Thus, frequencies
greater than one-half the sampling frequency (the Nyquist limit)
will not be sampled accurately. Attempting to surpass this
limit will introduce errors into the sampled waveform. To get
around this limitation in practical sampling systems (e.g.
digital Compact Disc recording and playback, etc.), engineers
utilize low-pass filters which attenuate signals of frequencies
higher than the Nyquist limit prior to digital sampling. For
CDs, which have a sampling rate of 44.1 kHz, these low-pass
filters begin attenuating at about 21 kHz and have reduced the
signal at the Nyquist limit of 22.05 kHz almost entirely. Thus,
a CD can have data sampled upto about 22 kHz, but no higher, and
is thus "bandwidth-limited." Sampling any higher frequencies
would create undesirable noise in the output. (Note that 44.1
kHz is chosen because it is about the lowest value for
accurately sampling the spectrum of frequencies important in
musical signals; about 15 Hz to 20 kHz. Any higher value would
be unnecessary and would require an increase data storage, an
obvious limitation for a system with two digital PCM channels
and error-correction data included with the PCM data).

The second limitation to accurate PCM sampling is related to the
number of bits each sample contains. CD sampled material is 16-
bit PCM. This corresponds to a dynamic range of about 96
decibels (dB). Systems using smaller sized samples will
obviously have lower dynamic range. Thus, they will have an
inherently higher level of noise, and are inappropriate for
high-fidelity reproduction of sound. Of course, the tradeoff
between sound quality and data storage is an important design
consideration in any PCM system.


Lesson 2. Superposition:

It is fairly easy to figure out what a sinewave looks like from
an oscilloscope plot. You've likely heard what a sinewave
sounds like; pretty boring. A simple sinewave can produce a
tone. However, most sound that we hear and find pleasing has
much more content than a single sinewave. Musical instruments
produce waveforms with a fundamental (do not confuse with my
terminology below; fundamental here refers to the primary
frequency created by an instrument) frequency (sinewave) and a
group of harmonic sinewaves at multiples of the fundamental.
These "harmonics" give each instrument a recognizable sound, or
timbre. When we break up the waveform, it is possible to show
that the original waveform is thus the addition of the
fundamental sinewave and all of its harmonic sinewaves. This
addition is called superposition. Of course, superposition can
be both additive, or destructive. Destructive superposition can
actually cancel out the amplitudes of two waveforms (for
example, if both are of the same frequency and amplitude, but
are 90 degrees out of phase; i.e. the peak of the first wave
corresponds to the trough of the second). If you listen to two
sinewaves being reproduced by the FM chip on your sound card (if
you have one), you will be listening to superimposed sinewaves.
However, since your ear is able to pull out the discrete
frequencies in the signal, you hear two sinewaves (sort of like
the FFT routine described below). Pretty neat, huh?


Lesson 3. Fourier Transforms:

Many of you may have seen programs that take PCM sampled sound
and output the data on the screen in an oscilloscope-like
display. In this case, the plot is based on amplitude vs. time.
It is possible to determine the relative frequency of a
continuous waveform by varying the sweep-rate of such an
oscilloscope, but there are better ways to gather frequency data
from PCM samples. One approach is to pass the data through an
algorithm called a Fast Fourier Transform (FFT) and to plot the
resulting data as a magnitude vs. frequency plot. Essentially,
the FFT loops thru a group of samples and pulls out all of the
sinewaves contained in the sampling period (delta). Since we
know the sampling rate (and thus, the time between samples), it
is possible to assign frequency values to the sinewaves
determined by the FFT. So, the Fourier Transform works like the
superposition principle in reverse.

The first sinewave is the lowest possible sinewave that can be
sampled accurately by the FFT, and corresponds to the sampling
frequency divided by the number of samples in delta. I refer to
this as the fundamental frequency of the plot, F-fund. The
frequency band plotted is twice F-fund, and the next is 3X, and
so on. Thus, the plotted bands continue up by multiples of F-
fund until we reach the Nyquist limiting frequency (F-Ny), which
incidentally has a band number equal to 1/2 the number of
samples in delta (band number is also called channel number; one
band is plotted for each frequency displayed by MicFFT for a
particular sampling frequency). Generally, data is plotted in
by linear scaling along the frequency axis, which means that
each band plotted represents one frequency. However, it should
be noted that frequencies located between two discrete bands
will be represented as maximal deflections of the two bands on
either side of this intervening frequency. Additionally, even
band-centered frequencies will produce deflections of those
bands adjacent to it, tapering off as we move in either
direction from the center band. One should use care when
ascribing a particular group of frequencies to a particular
plot; as you work with MicFFT, you will get an idea of what to
expect.


MicFFT Usage:

When you start MicFFT with the appropriate command line
arguments (see Options), you will be able to begin sampling
signals entering the ADC. Whistle into a microphone connected
to the sound card. You should see a peak, corresponding to the
frequency of the whistle. Since the whistle is produced at the
lips, there is little interaction with the resonant cavities or
the body that normally introduce harmonics in speech and
singing. Thus, whistling approximates a sinewave and can be
used to test MicFFT for simple waveforms. Vary the pitch of
your whistling. By reducing the pitch, you should see this peak
move to the left, thus indicating that the frequency is moving
lower. By raising the pitch, the peak should move to the right
of the plot (note that the degree of peak movement will be
affected by the chosen sampling frequency). Note the ruler at
the bottom of the plot screen. This ruler corresponds to the
multiples of the fundamental frequency. Ones are in white,
fives are in orange and tens are in red. You can use the ruler
to get an instant idea of the relative frequency during
sampling.

It is now possible to freeze the plot by pressing the SPACE bar.
Do this while continuing to whistle. Once the sampling has
stopped and you have a frozen waveform frequency spectrum on the
screen, you can use the arrow keys to move a cursor line around
the plot. Move the cursor line to the peak. Below, you will
see the frequency for this peak, as well as the amplitude (in
magnitude returned by the FFT routine, and based on a maximum of
128; and in dB, relative to a dynamic range from 1 to 48 dB for
the 8-bit samples). Note that the power density is related to
the area under the peak; the magnitudes shown are plotted only
for the band under the cursor line.


Keys for Freeze Window:

To move the cursor line around, use the LEFT or RIGHT keys for
single band movements. To speed up movement around the plot,
use CTRL-LEFT or CTRL-RIGHT to move ten bands for each keypress.
To move to the right end of the plot, press END or PGDN; to move
all the way to the left side, use HOME or PGUP. To return to
the sampling mode, press the SPACE bar. To exit the program at
any time (during sampling or in the freeze window), press ESC.


Options:

MicFFT v1.1 has a number of options which are set by command
line arguments using specified flags. Typing MicFFT at the DOS
prompt without any flags will run the program with the built-in
defaults. You can override these defaults using the following
flags:

-?:Display command line options. Also -h.

-f:Change the sampling frequency. -f25000 will change the
sampling frequency to 25 kHz. Valid range is from 1 Hz (which
would take 256 seconds before the first plot; not
recommended...) and the highest sampling rate of your sound
card. For the Covox card, this is 25000 (although I've been
able to get upto 34090 Hz before getting sampling problems).
For the Sound Blaster, this is 13 kHz. Play around with this
option a lot. Default is 10000.

-p:Change the default ADC port address. Default is 22F
(hex), which corresponds to the default for the Covox card.
-f24fh will change to port address/offset 24F. If you use the
Sound Blaster mode (see -b flag), the default will be 220. Note
that if you supply a hexidecimal address, use an "h" following
the address to signify that you are using a hex address.
Decimal addresses are assumed and require no h at the end.
MicFFT will report the address chosen, so you can verify that
the correct address has been selected.

-b:Change the sound card mode. The default is the Covox mode
(see Sound Cards, above), which is -b0. For Sound Blaster mode,
use -b1.

-m:Scaling mode. Set to -m1 for maximum amplitude mode. In
this mode, all amplitude data is scaled on the plot so that all
amplitudes are scaled to the amplitude of the highest peak in
the plot. Set to -m0 for log amplitude scaling; log amplitude
scaling is in dB, so this may be more suitable for some signals.
-m0 is the default.

-l:Line mode. Set to -l1 for line plot mode. In this mode,
lines are drawn between points corresponding to amplitudes of
successive bands. Set to -l0 for bar graph mode. In this mode,
each frequency band is plotted distinctly, from the bottom of
the plot to each amplitude point. -l0 is the default.

-c:Cosine envelope. Type -c to turn on the cosine envelope
function. This adds a filter that prescales the data to remove
discontinuities at either end of the waveform before entering
the FFT routine. This may produce a more stable display for
waveforms that are not rapidly changing.

-s:Squelch mode. To remove low-level noise from the input,
to clean up the plot, use -s. This removes any samples above
and below the zero level. For example, -s10 will remove all PCM
samples between -10 and +10. Use this option carefully, since
it can create square-waves, which have a variety of frequency
components that will polute the spectrum. I do not recommend
values of greater than, say 10.

-e and -v: Force EGA or VGA mode, respectively. The default is
to use the maximum resolution of the monitor, based on the mode
the monitor is set to when MicFFT is started. -e has lower
resolution, but may be faster on some VGA monitors. It also
produces a somewhat larger plot. -v was added for consistency.


Note that all options can be entered using either the - or /
switch. Case is not important, but you must not include white
spaces for any particular flag, since MicFFT will ignore any
options that do not conform to this syntax. Order is also not
important, since the options are collected and verified before
any setup occurs. I recommend that you create a batch file that
has your required configuration (for example, port and plotting
mode specs) along with a couple of %n's to allow additional
parameters to be entered. For example:

@echo off
REM RUNMF.BAT -- Batch for running MicFFT with set config.
MicFFT -p24Fh -m1 -l1 %1 %2 %3 %4


Performance:

As stated above, MicFFT frequency accuracy should not be
affected by different machine speeds, since the sampling is done
in a loop separate from the other routines, and uses the PC's
internal 8253 timer chip for accuracy in the fractional
microsecond range. However, the Sound Blaster mode may not be
as accurate, because a number of additional steps are required
for sampling that will certainly increase the processing time.
Since I don't have a Sound Blaster card, it is not possible for
me to test this out. Chris Pye, who wrote the Sound Blaster
routines, has observed frequency problems, but only when the
sampling rate exceeded 13 kHz. Try this out for yourself. If
you use a tuning fork (or some other stable frequency
generator), you should get approximately similar frequency peaks
at different sampling frequencies. Keep in mind that any error
introduced by this extra processing will affect high sampling
rates. Thus, compare low rates with very high rates. If the
frequency display is stable, everything should be pretty
accurate.

I have noticed that it is not possible to sample at a rate
higher than 34090 Hz (not even 34091!) on the Covox card. When
I try, I get no output (or output only in the first channel). I
don't know why this occurs, but it is probably related to the
way in which this specific card samples data, as well as my
machine's clock rate (33 MHz). I assumed that I would see a
smearing of frequency at these higher rates (i.e. shifting to
reflect a lower actual rate than set by the program), but rather
this is a brick wall beyond which sampling doesn't work. In a
way, I much prefer this, since it helps to know that the program
is accurate upto this maximum rate. Again, it is important to
test this out on each system, if you are really interested in
accurate, high-frequency sampling. In this respect, I may move
to DMA-controlled PCM sampling support for the Sound Blaster Pro
and Pro Audio Spectrum cards in future releases. This would
allow the card itself to do the work of ensuring accurate
sampling.

The refresh rate (i.e. the amount of time between sampling
periods) will vary, depending on the machine used. MicFFT is
set up to sample 256 data points, transform and plot out the
data, then repeat this. On slow machines, the refresh rate will
be low; on fast machines, particularly those equipped with
floating-point coprocessors, it will be impressively fast.
Nevertheless, it is important to stress that the time spent
processing the data is time spent not sampling. Thus, events
occuring between sampling periods will be ignored. This may be
problematic with time-varying signals, particularly those with
substantial transient components. For this reason, it is
necessary to carefully analyze the plot for such data.
Sometimes, the plot will miss these transients. If this is a
concern, I suggest slowing the sampling rate to the lowest
allowable frequency (based on the bandwidth of the signal), as
this will increase the chance of capturing this varying signal
in the plot. You can freeze a waveform, but this is difficult
with rapidly varying signals. A more suitable approach would be
to sample the waveform and select the parts of a time-domain
plot to display. Future versions of MicFFT will include this
feature.

Also, it is important to be cognisant of the Nyquist limit for
any particular waveform, as signals with components exceding the
limit will be "aliased" and not represented properly by the
spectral plot. As I described above, frequencies above the
Nyquist limit will produce errors (aliasing) unless these are
removed from the signal before they are sampled. This is
interesting in one respect: set the sampling frequency to about
4000 Hz and start a low whistle. As you increase the frequency,
you will notice that the peak wraps around as it moves to the
right side of the plot. If it goes far enough, it will also
wrap around the left side of the plot as well. This a very
graphic demonstration of aliasing. Beyond the academics,
aliasing can be a serious problem to accurate spectral analysis.
MicFFT does not yet have any low-pass filtering built in
(although future versions may include this), so you will have to
consider aliasing when chosing a particular signal and sampling
frequency. If you are capable, it is possible to add a low-pass
filter to the input line of your signal generator (or mic) to
eliminate these higher frequencies. Your sound card may even
provide for this type of bandwidth limiting.

MicFFT is not designed to compete with hardware spectrum
analyzers (not yet, at least). But, since these hardware
analyzers are many thousands of dollars, the cost of this
approach is more suitable for many applications, particularly
experimentation into sound, etc. Although you may come up with
interesting ideas, I don't yet recommend its use in building the
next-generation Hubble Telescope, etc. However, it might work
really well in analysis of Cold Fusion experiments. If you have
any results in this arena, please inform me so I can buy the
stock (SEC, just kidding already!).


Acknowledgements:

I'd like to take this opportunity to thank many of you who
downloaded MicFFT v1.0 and sent comments to me about the
program. Since I've not asked for a fee for the program, at
least it gives me a little ego boost, and makes the programming
a bit more fun.

Chris Pye has been instrumental in making MicFFT compatible with
Sound Blaster cards. He also provided code for the freeze-
window routine and the cosine-envelope option. He knows a lot
more about Fourier transforms than I (I'm a molecular biology
grad student, and just a engineering/physics hacker), and I'll
be working with him a lot in the future for developing better
versions of the program. Additionally, the FFT routines used in
this program are based on those implemented in an archive called
FFT.ARC found on Compuserve by the author Steve Sampson, as well
as those found in a book called Numerical Recipes in C. The
8253 timer routines (which have extraordinary accuracy) are
based on a Compuserve archive called TIMERH.ARC, uploaded by the
author Micahel Walraven. I am indebted to the work of these
authors.


Conditions of Use:

MicFFT v1.1 is currently freeware. However, if you use the
program, I ask that you contact me (see Contacting The Author)
to let me know what you are using it for, its fitness, and other
comments, problems, etc. When and if I decide to charge for my
work, I'll won't require this. When I download programs I find
interesting, I often notify the author out of courtesy.
Usually, I get lots of ideas as well. You may find that such
discourse can be fun and interesting. On the other hand, you
may find that I don't know anything about this stuff. You'll
just have to try me. The code for MicFFT is not available,
unless you make specific arrangements with me first. The main
reason for this is that I want to maintain control of the
development of the program. If you have good ideas, and are
proficient in coding, I'll make exceptions.


Contacting The Author:

You can contact me at the following E-mail addresses:

Compuserve 70701,1614
Internet: [email protected]

(Incidentally, I don't work for NASA, but our mail-server here
at UCLA is named this way for some strange reason).

I can also be contacted by phone at my Los Angeles residence at

(310) 390-0598

Please note that I'd prefer to get mail via Internet, since my
Compuserve bills have been skyrocketing (and I'm just a poor,
lowly grad student). You can do this on Compuserve by sending
the mail to an Internet address (see the help screen for the
specifics here. I don't recall offhand how to do this). I
check my mail on the Internet on a daily basis; I check
Compuserve much less frequently. For a super low-tech approach,
you can send a post via hardcopy to:

Craig Walsh
12420 Woodgreen Ave #203
Los Angeles, CA 90066


Have fun, and good luck with MicFFT v1.1. Let me know what you
think.


 December 25, 2017  Add comments

Leave a Reply