| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>Time/Frequency Transformation</title> |
| 3 |
<link rel="stylesheet" type="text/css" href="nyquiststyle.css"> |
| 4 |
<link rel="icon" href="nyquist-icon.png" /> |
| 5 |
<link rel="shortcut icon" href="nyquist-icon.png" /> |
| 6 |
</head> |
| 7 |
<body bgcolor="ffffff"> |
| 8 |
<a href = "part10.html">Previous Section</a> | <a href = "part12.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a> |
| 9 |
<hr> |
| 10 |
<a name = "121"><h2>Time/Frequency Transformation</h2></a> |
| 11 |
<p>Nyquist provides functions for FFT and inverse FFT operations on |
| 12 |
streams of audio data. Because sounds can be of any length, but an |
| 13 |
FFT operates on a fixed amount of data, FFT processing is typically |
| 14 |
done in short blocks or windows that move through the audio. Thus, a |
| 15 |
stream of samples is converted in to a sequence of FFT frames |
| 16 |
representing short-term spectra.</p> |
| 17 |
|
| 18 |
<p>Nyquist does not have a special data type corresponding to a sequence |
| 19 |
of FFT frames. This would be nice, but it would require creating a |
| 20 |
large set of operations suitable for processing frame |
| 21 |
sequences. Another approach, and perhaps the most “pure” would be to |
| 22 |
convert a single sound into a multichannel sound, with one channel per |
| 23 |
bin of the FFT.</p> |
| 24 |
|
| 25 |
<p>Instead, Nyquist violates its “pure” functional model and resorts to |
| 26 |
objects for FFT processing. A sequence of frames is represented by an |
| 27 |
XLISP object. Whenever you send the selector <code>:next</code> to the |
| 28 |
object, you get back either NIL, indicating the end of the sequence, |
| 29 |
or you get an array of FFT coefficients.</p> |
| 30 |
|
| 31 |
<p>The Nyquist function <code>snd-fft</code> (mnemonic, isn't it?) returns one |
| 32 |
of the frame sequence generating objects. You can pass any frame |
| 33 |
sequence generating object to another function, <code>snd-ifft</code>, and |
| 34 |
turn the sequence back into audio.</p> |
| 35 |
|
| 36 |
<p>With <code>snd-fft</code> and <code>snd-ifft</code>, you can create all sorts of |
| 37 |
interesting processes. The main idea is to create intermediate objects |
| 38 |
that both accept and generate sequences of frames. These objects can |
| 39 |
operate on the frames to implement the desired spectral-domain |
| 40 |
processes. Examples of this can be found in the file |
| 41 |
<code>nyquist/lib/fft/fft_tutorial.htm</code><a name="index929"></a><a name="index930"></a><a name="index931"></a>, which is part of the |
| 42 |
standard Nyquist release. The documentation for <code>snd-fft</code> and |
| 43 |
<code>snd-ifft</code> follows.</p> |
| 44 |
<dl> |
| 45 |
<dt> |
| 46 |
<code>snd-fft(<a name="index932"></a><a name="index933"></a><i>sound</i>, <i>length</i>, |
| 47 |
<i>skip</i>, <i>window</i>)</code> [SAL]<br> |
| 48 |
|
| 49 |
<code>(snd-fft <i>sound</i> <i>length</i> <i>skip</i> <i>window</i>)</code> |
| 50 |
[LISP]</dt> |
| 51 |
<dd>This function performs an FFT on the first samples in |
| 52 |
<i>sound</i> and returns a Lisp array of <code>FLONUM</code>s. The function |
| 53 |
modifies the <i>sound</i>, violating the normal rule that sounds are |
| 54 |
immutable in Nyquist, so it is advised that you copy the sound using |
| 55 |
<code>snd-copy</code> if there are any other references to <i>sound</i>. The |
| 56 |
length of the FFT is specified by <i>length</i>, a <code>FIXNUM</code> |
| 57 |
(integer) which must be a power of 2. After each FFT, the sound is |
| 58 |
advanced by <i>skip</i> samples, also of type <code>FIXNUM</code>. Overlapping |
| 59 |
FFTs, where <i>skip</i> is less than <i>length</i>, are allowed. If |
| 60 |
<i>window</i> is not <code>NIL</code>, it must be a sound. The first |
| 61 |
<i>length</i> samples of <i>window</i> are multiplied by <i>length</i> samples |
| 62 |
of <i>sound</i> before performing the FFT. When there are no more samples |
| 63 |
in <i>sound</i> to transform, this function returns <code>NIL</code>. The |
| 64 |
coefficients in the returned array, in order, are the DC coefficient, |
| 65 |
the first real, the first imaginary, the second real, the second |
| 66 |
imaginary, etc. The last array element corresponds to the real |
| 67 |
coefficient at the Nyquist frequency.<br><br> |
| 68 |
<dt><code>snd-ifft(<a name="index934"></a><a name="index935"></a><a name="index936"></a><i>time</i>, <i>srate</i>, <i>iterator</i>, <i>skip</i>, <i>window</i>)</code> [SAL]<br> |
| 69 |
|
| 70 |
<code>(snd-ifft <i>time</i> <i>srate</i> <i>iterator</i> <i>skip</i> |
| 71 |
<i>window</i>)</code> [LISP]</dt> |
| 72 |
<dd>This function performs an IFFT on a sequence |
| 73 |
of spectral frames obtained from <i>iterator</i> and returns a sound. The |
| 74 |
start time of the sound is given by <i>time</i>. Typically, this would be |
| 75 |
computed by calling <code>(local-to-global 0)</code>. The sample rate is |
| 76 |
given by <i>srate</i>. Typically, this would be <code>*sound-srate*</code>, but |
| 77 |
it might also depend upon the sample rate of the sound from which the |
| 78 |
spectral frames were derived. To obtain each frame, the function sends |
| 79 |
the message <code>:next</code> to the <i>iterator</i> object, using XLISP's |
| 80 |
primitives for objects and message passing. The object should return |
| 81 |
an array in the same format as obtained from <code>snd-fft</code>, and the |
| 82 |
object should return <code>NIL</code> when the end of the sound is |
| 83 |
reached. After each frame is inverse transformed into the time domain, |
| 84 |
it is added to the resulting sound. Each successive frame is added |
| 85 |
with a sample offset specified by <i>skip</i> relative to the previous |
| 86 |
frame. This must be an integer greater than zero and less than the |
| 87 |
frame (FFT) size. If <i>window</i> is not <code>NIL</code>, it must be a |
| 88 |
sound. This window signal is multiplied by the inverse transformed |
| 89 |
frame before the frame is added to the output sound. The length of |
| 90 |
each frame should be the same power of 2. The length is implied by |
| 91 |
the first array returned by <i>iterator</i>, so it does not appear as a |
| 92 |
parameter. This length is also the number of samples used from |
| 93 |
<i>window</i>. Extra samples are ignored, and window is padded with zeros |
| 94 |
if necessary, so be sure <i>window</i> is the right length. The resulting |
| 95 |
sound is computed on demand as with other Nyquist sounds, so |
| 96 |
<code>:next</code> messages are sent to <i>iterator</i> only when new frames |
| 97 |
are needed. One should be careful not to reuse or modify <i>iterator</i> |
| 98 |
once it is passed to <code>snd-ifft</code>. </dd></dl><a name = "122"><h3>Spectral Processing</h3></a><a name="index937"></a> |
| 99 |
<p>There are a number of functions defined to make spectral processing |
| 100 |
easier in XLISP and SAL. The general approach, as described above, is |
| 101 |
to create an iterator object that returns spectral frames. To avoid |
| 102 |
using the XLISP object system directly, a more functional interface is |
| 103 |
defined, especially for SAL users. |
| 104 |
The <code>sa-init</code> function creates an iterator, and |
| 105 |
<code>sa-next</code> retrieves spectral frames. Various functions are also |
| 106 |
provided to transform these into amplitude (magnitude) spectra, plot |
| 107 |
them and perform other operations.</p> |
| 108 |
|
| 109 |
<p>Some examples that use these spectral processing functions can be found |
| 110 |
in the Nyquist extension “fftsal” (use the |
| 111 |
NyquistIDE's Window : Nyquist Extensions menu item to download it; it |
| 112 |
will then be in your <code>nyquist/lib/fftsal</code> directory. You can find |
| 113 |
descriptions of the examples in <code>nyquist/lib/fftsal/spectral-process.lsp</code> |
| 114 |
and <code>nyquist/lib/fftsal/spectral-process.sal</code>.</p> |
| 115 |
<dl> |
| 116 |
<dt> |
| 117 |
<code>sa-init(<a name="index938"></a><a name="index939"></a>resolution: |
| 118 |
<i>hz</i>, fft-dur: <i>dur</i>, skip-period: <i>skip</i>, window: <i>window-type</i>, |
| 119 |
input: <i>input</i>)</code> [SAL]<br> |
| 120 |
|
| 121 |
<code>(sa-init :resolution <i>hz</i> :fft-dur <i>dur</i> |
| 122 |
:skip-period <i>skip</i> :window <i>window-type</i> |
| 123 |
:input <i>input</i>)</code> [LISP]</dt> |
| 124 |
<dd>Creates a spectral-analysis object |
| 125 |
that can be used to obtain spectral data from a sound. All keyword |
| 126 |
parameters are optional except <code>input</code>. The <code>resolution</code> |
| 127 |
keyword parameter gives the width of each spectral bin in Hz. It may |
| 128 |
be <code>nil</code> or not specified, in which case the resolution is |
| 129 |
computed from <code>fft-dur</code>. The actual resolution may be finer than |
| 130 |
the specified resolution because fft sizes are rounded to a power of |
| 131 |
2. The <code>fft-dur</code> is the width of the FFT window in seconds. The |
| 132 |
actual FFT size will be rounded up to the nearest power of two in |
| 133 |
samples. If <code>nil</code>, <code>fft-dur</code> will be calculated from |
| 134 |
<code>resolution</code>. If both <code>fft-size</code> and <code>resolution</code> are |
| 135 |
<code>nil</code> or not specified, the default value is 1024 samples, |
| 136 |
corresponding to a duration of 1024 / signal-sample-rate. If both |
| 137 |
<code>resolution</code> and <code>fft-dur</code> are specified, the |
| 138 |
<code>resolution</code> parameter will be ignored. Note that <code>fft-dur</code> |
| 139 |
and <code>resolution</code> are reciprocals. The <code>skip-period</code> |
| 140 |
specifies the time interval in seconds between successive spectra (FFT |
| 141 |
windows). Overlapping FFTs are possible. The default value overlaps |
| 142 |
windows by 50%. Non-overlapped and widely spaced windows that ignore |
| 143 |
samples by skipping over them entirely are also acceptable. The |
| 144 |
<code>window</code> specifies the type of window. The default is raised |
| 145 |
cosine (Hann or "Hanning") window. Options include <code>:hann</code>, |
| 146 |
<code>:hanning</code>, <code>:hamming</code>, <code>:none</code> or <code>nil</code>, where |
| 147 |
<code>:none</code> and <code>nil</code> mean a rectangular window. The |
| 148 |
<code>input</code> can be a string (which specifies a sound file to read) or |
| 149 |
a Nyquist SOUND to be analyzed. The return value is an XLISP object |
| 150 |
that can be called to obtain parameters as well as a sequence of |
| 151 |
spectral frames. Normally, you will set a variable to this result and |
| 152 |
pass the variable to <code>sa-next</code>, described below.<br><br> |
| 153 |
<dt><code>sa-info(<a name="index940"></a><i>sa-obj</i>)</code> [SAL]<br> |
| 154 |
|
| 155 |
<code>(sa-info <i>sa-obj</i>)</code> [LISP]</dt> |
| 156 |
<dd>Prints information |
| 157 |
about an <i>sa-obj</i>, which was created by <code>sa-init</code> (see |
| 158 |
above). The return value is <code>nil</code>, but information is printed.<br><br> |
| 159 |
<dt><code>sa-next(<a name="index941"></a><i>sa-obj</i>)</code> [SAL]<br> |
| 160 |
|
| 161 |
<code>(sa-next <i>sa-obj</i>)</code> [LISP]</dt> |
| 162 |
<dd>Fetches the next |
| 163 |
spectrum from <i>sa-obj</i>, which was created by <code>sa-init</code> (see |
| 164 |
above). The return value is an array of FLONUMs representing the |
| 165 |
discrete complex spectrum.<br><br> |
| 166 |
<dt><code>sa-magnitude(<a name="index942"></a><i>frame</i>)</code> [SAL]<br> |
| 167 |
|
| 168 |
<code>(sa-magnitude <i>frame</i>)</code> [LISP]</dt> |
| 169 |
<dd>Computes the magnitude |
| 170 |
(amplitude) spectrum from a frame returned by <code>sa-frame</code>. The |
| 171 |
<i>i</i><sup T>th</sup> bin is stored at index <i>i</i>. The size of the array is |
| 172 |
the FFT size / 2 + 1.<br><br> |
| 173 |
<dt><code>sa-normalize(<a name="index943"></a><i>frame</i> |
| 174 |
[, <i>max</i>])</code> [SAL]<br> |
| 175 |
|
| 176 |
<code>(sa-normalize <i>frame</i> [<i>max</i>])</code> [LISP]</dt> |
| 177 |
<dd>Normalize |
| 178 |
a copy of <i>frame</i>, a magnitude (amplitude) spectrum returned by |
| 179 |
<code>sa-magnitude</code>. If <i>max</i> (a FLONUM) is provided, the spectrum |
| 180 |
will be normalized to have a maximum value of <i>max</i>, which defaults |
| 181 |
to 1.<br><br> |
| 182 |
<dt><code>sa-plot(<a name="index944"></a><i>sa-obj</i>, <i>frame</i>)</code> [SAL]<br> |
| 183 |
|
| 184 |
<code>(sa-plot <i>sa-obj</i> <i>frame</i>)</code> [LISP]</dt> |
| 185 |
<dd>Plots a magnitude |
| 186 |
(amplitude) spectrum from <i>frame</i> returned by <code>sa-magnitude</code>. |
| 187 |
The <i>sa-obj</i> parameter should be the same value used to obtain the frame.<br><br> |
| 188 |
<dt><code>sa-print(<a name="index945"></a><i>file</i>, <i>sa-obj</i>, <i>frame</i>, |
| 189 |
cutoff: <i>cutoff</i>, threshold: <i>threshold</i>)</code> [SAL]<br> |
| 190 |
|
| 191 |
<code>(sa-print <i>sa-obj</i> <i>file</i> <i>frame</i> |
| 192 |
:cutoff <i>cutoff</i> :threshold <i>threshold</i>)</code> [LISP]</dt> |
| 193 |
<dd>Prints an ASCII |
| 194 |
plot of <i>frame</i>, a magnitude (amplitude) spectrum returned by |
| 195 |
<code>sa-magnitude</code> (or <code>sa-normalize</code>). The <i>file</i> is either a |
| 196 |
file opened for writing or <code>T</code> to print to the console. The |
| 197 |
caller is responsible for closing the file (eventually). The |
| 198 |
<i>sa-obj</i> parameter should be the same value used to obtain the |
| 199 |
frame. If <i>cutoff</i>, a FLONUM, is provided, only the spectrum below |
| 200 |
cutoff (Hz) will be printed. If <i>threshold</i>, a FLONUM, is provided, |
| 201 |
the output may elide bins with values below the threshold.<br><br> |
| 202 |
<dt><code>sa-get-bin-width(<a name="index946"></a><i>sa-obj</i>)</code> |
| 203 |
[SAL]<br> |
| 204 |
|
| 205 |
<code>(sa-get-bin-width <i>sa-obj</i>)</code> [LISP]</dt> |
| 206 |
<dd>Returns |
| 207 |
the width of a frequency bin as a FLONUM in Hz (also the separation |
| 208 |
of bin center frequencies). The center frequency of the <i>i</i><sup T>th</sup> |
| 209 |
bin is <code>i * bin-width</code>.<br><br> |
| 210 |
<dt><code>sa-get-fft-size(<a name="index947"></a><i>sa-obj</i>)</code> |
| 211 |
[SAL]<br> |
| 212 |
|
| 213 |
<code>(sa-get-fft-size <i>sa-obj</i>)</code> [LISP]</dt> |
| 214 |
<dd>Returns a FIXNUM, |
| 215 |
the size of the FFT, a power of 2. <br><br> |
| 216 |
<dt><code>sa-get-fft-dur(<a name="index948"></a><i>sa-obj</i>)</code> |
| 217 |
[SAL]<br> |
| 218 |
|
| 219 |
<code>(sa-get-fft-dur <i>sa-obj</i>)</code> [LISP]</dt> |
| 220 |
<dd>Returns a FIXNUM, |
| 221 |
the duration of the FFT window.<br><br> |
| 222 |
<dt><code>sa-get-fft-window(<a name="index949"></a><i>sa-obj</i>)</code> |
| 223 |
[SAL]<br> |
| 224 |
|
| 225 |
<code>(sa-get-fft-window <i>sa-obj</i>)</code> [LISP]</dt> |
| 226 |
<dd>Returns a symbol |
| 227 |
representing the type of window used, <code>:hann</code>, <code>:hamming</code> or |
| 228 |
<code>:none</code>.<br><br> |
| 229 |
<dt><code>sa-get-skip-period(<a name="index950"></a><i>sa-obj</i>)</code> |
| 230 |
[SAL]<br> |
| 231 |
|
| 232 |
<code>(sa-get-skip-period <i>sa-obj</i>)</code> [LISP]</dt> |
| 233 |
<dd>Returns the |
| 234 |
skip size in seconds (a FLONUM).<br><br> |
| 235 |
<dt><code>sa-get-fft-skip-size(<a name="index951"></a><i>sa-obj</i>)</code> |
| 236 |
[SAL]<br> |
| 237 |
|
| 238 |
<code>(sa-get-fft-skip-size <i>sa-obj</i>)</code> [LISP]</dt> |
| 239 |
<dd>Returns the |
| 240 |
skip size in samples (a FIXNUM).<br><br> |
| 241 |
<dt><code>sa-get-sample-rate(<a name="index952"></a><i>sa-obj</i>)</code> |
| 242 |
[SAL]<br> |
| 243 |
|
| 244 |
<code>(sa-get-sample-rate <i>sa-obj</i>)</code> [LISP]</dt> |
| 245 |
<dd>Returns the |
| 246 |
sample rate of the sound being analyzed (a FLONUM) in Hz. |
| 247 |
</dd></dl><hr> |
| 248 |
<a href = "part10.html">Previous Section</a> | <a href = "part12.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a> |
| 249 |
</body></html> |