SigPack - the C++ signal processing library
spectrum.cpp
Go to the documentation of this file.
1 
8 #include "sigpack.h"
9 using namespace sp;
10 
11 // Specs from http://soundfile.sapp.org/doc/WaveFormat/
12 typedef struct WAV_HEADER {
13  uint8_t RIFF[4]; // RIFF Header Magic header
14  uint32_t chunkSize; // RIFF Chunk Size
15  uint8_t WAVE[4]; // WAVE Header
16  uint8_t fmt[4]; // FMT header
17  uint32_t subchunk1Size; // Size of the fmt chunk
18  uint16_t audioFormat; // Audio format
19  uint16_t numOfChan; // Number of channels
20  uint32_t samplesPerSec; // Sampling Frequency in Hz
21  uint32_t bytesPerSec; // bytes per second
22  uint16_t blockAlign; // 2=16-bit mono, 4=16-bit stereo
23  uint16_t bitsPerSample; // Number of bits per sample
24  uint8_t subchunk2ID[4]; // "data" string
25  uint32_t subchunk2Size; // Sampled data length
26 } wav_header;
27 
28 int main(int argc, char *argv[])
29 {
30  wav_header wavHeader;
31 
32  // Get THX DeepNote .wav file at
33  // http://www.orangefreesounds.com/wp-content/uploads/Zip/thx-sound-test.zip
34  FILE *wavFile = fopen("thx-sound-test.wav", "r");
35 
36  // Read the header
37  fread(&wavHeader, 1, sizeof(wav_header), wavFile);
38 
39  // Read the data
40  uint16_t Nb = wavHeader.bitsPerSample / 8; // Number of bytes per sample
41  uint32_t Ns = (wavHeader.chunkSize - 36) / Nb; // Number of samples
42 
43  arma::Mat<int16_t> x(wavHeader.numOfChan, Ns / wavHeader.numOfChan);
44  fread(x.memptr(), Nb, Ns, wavFile);
45  fclose(wavFile);
46 
47  // Get the left channel
48  arma::Col<int16_t> x_left = x.row(0).t();
49 
50  // Calculate the spectrogram
51  const int FFT_SIZE = 1024;
52  const int FFT_OVERLAP = 128;
53  arma::mat P = 10 * log10(abs(specgram(x_left, FFT_SIZE, FFT_OVERLAP)));
54 
55  // Plot
56  arma::mat Q =
57  P.rows(FFT_SIZE / 2, FFT_SIZE - 1); // Cut out the positive parts
58  gplot gp0;
59  gp0.window("Deep note spectrogram", 100, 100, 1200, 400);
60  gp0.send2gp("unset tics");
61  gp0.send2gp("unset colorbox");
62  gp0.image(Q);
63  return 0;
64 }
arma::mat specgram(const arma::Col< T1 > &x, const arma::uword Nfft=512, const arma::uword Noverl=256)
Power spectrogram calculation.
Definition: spectrum.h:114
uint16_t numOfChan
Definition: spectrum.cpp:19
Gnuplot class.
Definition: gplot.h:25
uint32_t samplesPerSec
Definition: spectrum.cpp:20
Definition: base.h:7
struct WAV_HEADER wav_header
uint16_t audioFormat
Definition: spectrum.cpp:18
uint32_t chunkSize
Definition: spectrum.cpp:14
uint32_t subchunk1Size
Definition: spectrum.cpp:17
uint16_t blockAlign
Definition: spectrum.cpp:22
void image(const arma::Mat< T > &x)
Plot mat as image.
Definition: gplot.h:473
uint16_t bitsPerSample
Definition: spectrum.cpp:23
uint32_t bytesPerSec
Definition: spectrum.cpp:21
uint32_t subchunk2Size
Definition: spectrum.cpp:25
int main(int argc, char *argv[])
Definition: spectrum.cpp:28
void window(const int fig, const char *name, const int x, const int y, const int width, const int height)
Configure the figure used Windows environment.
Definition: gplot.h:182
void send2gp(const char *cmdstr)
Send command to Gnuplot pipe.
Definition: gplot.h:141