Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a sound file with the extension wav. I got the amplitude values of this file. Then I generated values of Mexican hat wavelet using y = (1-t*t)*exp(-1*t*t/2). Now I need to get the spectrum of this audio file. How to do this. Here is my code

What I have tried:

C++
#include <iostream>
#include "WavData.cpp"
#include <graphics.h>
#include<stdio.h>
#include<math.h>

#define PI 3.141592653589793238462643383279502884197169399375105820974
using namespace std;
using namespace yazz::audio;
//drawing function
void DrawSamples(const double* source, int dataLen, int start, int finish){
 for(int i = start; i < finish; i++){
        if(i == start) moveto((getmaxx()-30)*i/(finish - start)+10, getmaxy()/2 - source[i]);
        else
        lineto((getmaxx()-30)*i/(finish - start)+10, getmaxy()/2 - source[i]);
        }
     }
/**************mexican hat(ricker) function*************************************/
double Mhat(double time, double a, double b)
{
   double mhat;
   mhat = 200*(2/(sqrt(3)*pow(PI, 1./4)))*(1-pow((time-b)/a,2))*exp(-1*pow((time-b)/a,2)/2);
   return mhat;
           }
/**************************************************/

int main(){    
WavData* wavData = WavData::readFromFile("son.wav");//read aduio file
const double *normalizedData = wavData->getNormalizedData();//to get values of audio file
int dataLength = wavData->getNumberOfSamples();//to get length of audio file values

/******************************************/
initwindow(1300, 600, "Mexican hat", 0, 0, false, true);
setbkcolor(WHITE);
cleardevice();
setcolor(CYAN);
/******************ricker*************************/
//generate mexican hat
double m = -5, n = 5, dt = 0.15625, a = 1, b = 0;//dt = 0.15625
int length = (n - m)/0.15625;//0.15625

double* time = new double[length];
//fill time[]
for(int i = 0; i < length; i++)
{
        time[i] = m;
        m = m + dt;
        }

double* mhat = new double[length];
//here I get values of mexican hat wavelet
for(int j = 0; j < length; j++){
mhat[j] = Mhat(time[j], a, b);
}

//now what need I to do

 delete[] normalizedData;
 delete[] time;
 delete[] mhat;

getch();    
return 0;
}
Posted
Updated 22-May-17 6:16am

1 solution

The usual technique for obtaining the spectrum of a signal to use a Fourier transform, most often the FFT. There is one example program at this page[^].

By the way, you might prefer to include math.h with the macro _USE_MATH_DEFINES defined. That file has definitions of most common mathematical constants like pi, e, etc. The value of pi is defined in the constant M_PI.
 
Share this answer
 
Comments
Member 12464144 23-May-17 0:22am    
I know to get spectrum using FFT. But, I have to get spectrum with mexican hat wavelet. Because this is my graduate work
Rick York 23-May-17 17:25pm    
If you know how to obtain the spectrum of a signal with an FFT then what is the problem? A signal is a signal. It could be random noise, it could be sine wave, it could a song from a CD. What difference does that make? As long as you have some data points to sample you should have everything you need to make it happen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900