Skip to content

Technical documentation

Rémi Georgiou edited this page Feb 24, 2017 · 12 revisions

MATLAB implementation of DFT and IDFT

% Example signal
t = 0:0.001:0.01;
N = length(t);
x = 0.5 + cos(2*pi*3000.*t) + 2*cos(2*pi*6000.*t);

% DFT
X = zeros(1,N);
for m=0:N-1
    Xm = 0;
        for n=0:N-1
            Xm = Xm + (x(n+1) * exp(-i*2*pi*m*n/N));
        end
    X(m+1) = Xm;
end

% IDFT
xx = zeros(1,N);
for n=0:N-1
    xn = 0;
    for m=0:N-1
        xn = xn + (X(m+1) * exp(i*2*pi*m*n/N));
    end
    xx(n+1) = 1/N * xn;
end

Wave file header format specification

Example:

Positions Sample Value Description Size
0-3 "RIFF" Marks the file as a riff file. Characters are each 1 byte long. 4 bytes
4-7 File size (integer) Size of the overall file - 8 bytes 4 bytes
8-11 "WAVE" File Type Header. For our purposes, it always equals "WAVE". 4 bytes
12-15 "fmt " Format chunk marker. Includes trailing null 4 bytes
16-19 16 Length of format data as listed above 4 bytes
20-21 1 Type of format (1 is linear PCM) 2 bytes
22-23 2 Number of Channels 2 bytes
24-27 44100 Sample Rate - Number of Samples per second [kSPS or Hz]. 4 bytes
28-31 176400 Bytes per second = speed of data stream (Sample Rate * BitsPerSample * Channels) / 8. 4 bytes
32-33 4 Block alignment = (BitsPerSample * Channels) / 8. 1 byte = 8 bit mono, 2 bytes = 8 bit stereo/16 bit mono, 4 bytes = 16 bit stereo. The number of bytes for one sample including all channels. 2 bytes
34-35 16 Quantization bits per sample 2 bytes
36-39 "data" "data" chunk header. Marks the beginning of the data section. 4 bytes
40-43 Size of data Number of bytes of data that is included in data section 4 bytes

8-bit samples are stored as unsigned bytes, ranging from 0 to 255.
16-bit samples are stored as 2's-complement signed integers, ranging from -32768 to 32767.

In general the header size is 44 bytes. There may be additional subchunks in Wave data stream. If so, each will have a 4 bytes (char) SubChunkID, 4 bytes of SubChunkSize and SubChunkSize amount of data. The rest is audio data.
Source:
http://www.topherlee.com/software/pcm-tut-wavformat.html
http://soundfile.sapp.org/doc/WaveFormat/

Caution: Order of samples in a stereo wave file

Sample 1 - Left Sample 1 - Right Sample 2 - Left Sample 2 - Right ...

Convert linear PCM data to float

16-bit PCM audio data is stored as signed 16-bit integers in the range -32768 to 32767 (or short in Java). If for some reason you are using >16 bit integers, we need additional bounding to make that sure the float values lie in the range [-1,1].
Convert to float:

short data = ... // get 2 bytes of PCM data
float f = ((float) data) / (float) 32768
if (f > 1) f = 1;
if (f < -1) f = -1;

Clone this wiki locally