?? convolution.cpp
字號:
///////////////////////////////////////////////////////////////////////////////
//
// FILE: convolution.cpp
//
// This program will demonstrate the process of convolution.
//
// The transfer function for the convolution algorithm is x[n] * h[n] = y[n]
//
// Where x[n] is the input samples
// h[n] is the input response
// y[n] is the output samples
//
//
///////////////////////////////////////////////////////////////////////////////
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
///////////////////////////////////////////////////////////////////////////////
// function prototypes
///////////////////////////////////////////////////////////////////////////////
void InitializeSineTable( float[], size_t );
void GenerateInputPulse( const float[], float[], size_t);
void GenerateImpulseCoeffs( const float[], float[], size_t );
void CalculateOutputPulse( const float[], size_t, const float[], size_t, float[] );
///////////////////////////////////////////////////////////////////////////////
// global variables
///////////////////////////////////////////////////////////////////////////////
float Table[360] = {0}; // Array for generated 0 to 360 degrees sine table
float Input[360] = {0}; // The generated input pulse array for 360 samples
float Output[396] = {0}; // The calculated output signal, for 396 samples
float Impulse[36] = {0}; // The generated impulse response array for 36 points
///////////////////////////////////////////////////////////////////////////////
// void main()
///////////////////////////////////////////////////////////////////////////////
int main(void)
{
InitializeSineTable( Table, sizeof(Table) );
GenerateInputPulse( Table, Input, sizeof(Table) );
GenerateImpulseCoeffs( Table, Impulse, sizeof(Impulse) );
CalculateOutputPulse( Input, sizeof(Input), Impulse, sizeof(Impulse), Output );
exit( 0 );
}
///////////////////////////////////////////////////////////////////////////////
// void InitializeSineTable( float[], size_t )
///////////////////////////////////////////////////////////////////////////////
void InitializeSineTable( float Table[], size_t nSize )
{
const float RADIANS = 0.017453292;
for( int i=0; i<nSize; i++ )
{
Table[i] = sin( RADIANS * i );
}
}
///////////////////////////////////////////////////////////////////////////////
// void GenerateInputPulse( const float[], float[], size_t )
///////////////////////////////////////////////////////////////////////////////
void GenerateInputPulse( const float Table[], float Input[], size_t nSize )
{
for( int i=1; i<=10; i++ )
{
for( int j=0; j<nSize/10; j++ )
{
Input[j*i] = Table[((j*i)/i)];
}
}
}
///////////////////////////////////////////////////////////////////////////////
// void GenerateImpulseCoeffs( const float[], float[], size_t )
///////////////////////////////////////////////////////////////////////////////
void GenerateImpulseCoeffs( const float Table[], float Impulse[], size_t nSize )
{
for( int i=0; i<nSize; i++ )
{
Impulse[i] = Table[(i*10)];
}
}
///////////////////////////////////////////////////////////////////////////////
// void CalculateOutputPulse( const float[], size_t, const float[], size_t, float[] )
///////////////////////////////////////////////////////////////////////////////
void CalculateOutputPulse( const float Input[], size_t nInputSize,
const float Impulse[], size_t nImpulseSize,
float Output[] )
{
for( int i=0; i<nInputSize; i++ )
{
for( int j=0; j<nImpulseSize; j++ )
{
Output[i+j] = Output[i+j] + (Input[i] * Impulse[j]);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -