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