如何使用墨西哥帽获取音频文件的频谱 [英] How to get spectrum of audo file using mexican hat

查看:78
本文介绍了如何使用墨西哥帽获取音频文件的频谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展名为wav的声音文件。我得到了这个文件的幅度值。然后我使用y =(1-t * t)* exp(-1 * t * t / 2)生成墨西哥帽小波的值。现在我需要获取此音频文件的频谱。这该怎么做。这是我的代码



我尝试过:



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:

#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;
}

推荐答案

获取信号频谱以使用傅里叶变换的常用技术,通常是FFT。 此页有一个示例程序[ ^ ]。



顺便说一下,你可能更喜欢包括math.h与宏_USE_MATH_DEFINES定义。该文件具有最常见的数学常数的定义,如pi,e等.pi的值在常量M_PI中定义。
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.


这篇关于如何使用墨西哥帽获取音频文件的频谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆