SigPack - the C++ signal processing library
 
Loading...
Searching...
No Matches
spectrum.cpp

Output

Source

#include "sigpack.h"
using namespace sp;
// Specs from http://soundfile.sapp.org/doc/WaveFormat/
typedef struct WAV_HEADER
{
uint8_t RIFF[4]; // RIFF Header Magic header
uint32_t chunkSize; // RIFF Chunk Size
uint8_t WAVE[4]; // WAVE Header
uint8_t fmt[4]; // FMT header
uint32_t subchunk1Size; // Size of the fmt chunk
uint16_t audioFormat; // Audio format
uint16_t numOfChan; // Number of channels
uint32_t samplesPerSec; // Sampling Frequency in Hz
uint32_t bytesPerSec; // bytes per second
uint16_t blockAlign; // 2=16-bit mono, 4=16-bit stereo
uint16_t bitsPerSample; // Number of bits per sample
uint8_t subchunk2ID[4]; // "data" string
uint32_t subchunk2Size; // Sampled data length
int main( int argc, char* argv[] )
{
wav_header wavHeader;
// Get THX DeepNote .wav file at
// http://www.orangefreesounds.com/wp-content/uploads/Zip/thx-sound-test.zip
const char* filename = "thx-sound-test.wav";
FILE* wavFile = fopen( filename, "r" );
if (wavFile == nullptr)
{
fprintf(stderr, "Error: Failed to open file '%s'\n", filename);
fprintf(stderr, "Download it at http://www.orangefreesounds.com/wp-content/uploads/Zip/thx-sound-test.zip");
return 1;
}
// Read the header
fread( &wavHeader, 1, sizeof( wav_header ), wavFile );
// Read the data
uint16_t Nb = wavHeader.bitsPerSample / 8; // Number of bytes per sample
uint32_t Ns = ( wavHeader.chunkSize - 36 ) / Nb; // Number of samples
arma::Mat<int16_t> x( wavHeader.numOfChan, Ns / wavHeader.numOfChan );
fread( x.memptr(), Nb, Ns, wavFile );
fclose( wavFile );
// Get the left channel
arma::Col<int16_t> x_left = x.row( 0 ).t();
// Calculate the spectrogram
const int FFT_SIZE = 1024;
const int FFT_OVERLAP = 128;
arma::mat P = 10 * log10( abs( specgram( x_left, FFT_SIZE, FFT_OVERLAP ) ) );
// Plot
arma::mat Q = P.rows( FFT_SIZE / 2, FFT_SIZE - 1 ); // Cut out the positive parts
gplot gp0;
gp0.window( "Deep note spectrogram", 100, 100, 1200, 400 );
gp0.send2gp( "unset tics" );
gp0.send2gp( "unset colorbox" );
gp0.image( Q );
return 0;
}
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