📄 spectrum_analyser.c
字号:
/*****************************************************************************/
/* */
/* FILENAME */
/* spectrum_analyser.c */
/* */
/* DESCRIPTION */
/* TMS320C5416 DSK. */
/* Spectrum analyser using Fast Fourier Transform (FFT). */
/* User switches select one of the frequency components to be displayed */
/* on the bargraph. */
/* */
/* Here N is the number of FFT terms and Fs is the sampling frequency of */
/* 48000 Hz. */
/* */
/* REVISION */
/* Revision: 1.00 */
/* Author : Richard Sikora */
/*---------------------------------------------------------------------------*/
/* */
/* HISTORY */
/* Revision 1.00 */
/* 2nd November 2002. Created by Richard Sikora from TMS320C5402 code. */
/* */
/*****************************************************************************/
#include <stdio.h> /* Required for functions printf() and puts() */
/*****************************************************************************/
/* */
/* The name of the following header file will change if used as part of a */
/* different project. The new project xxxx will use xxxxcfg.h instead. */
/* */
/*****************************************************************************/
#include "spectrum_analysercfg.h"
#include "dsk5416.h"
#include "dsk5416_pcm3002.h"
#include "bargraph.h"
#include "switches.h"
#include "fftcmplx.h"
/*****************************************************************************/
/* Configuration setup of registers of PCM3002 Codec */
/*****************************************************************************/
DSK5416_PCM3002_Config setup = {
0x1FF, // Set-Up Reg 0 - Left channel DAC attenuation
0x1FF, // Set-Up Reg 1 - Right channel DAC attenuation
0x0, // Set-Up Reg 2 - Various ctl e.g. power-down modes
0x0 // Set-Up Reg 3 - Codec data format control
};
/*****************************************************************************/
/* */
/* The following number N gives the size of the FFT. It must divide exactly */
/* into 512. Typical values are 4, 8, 16, 32, 64, 128, 256 and 512. */
/* */
/*****************************************************************************/
#define N 128
int input_buffer[N]; /* Buffer for input samples from codec */
COMPLEX y[N]; /* Variable passed to FFT and modified */
unsigned int power_spectrum[N]; /* Power of each frequency component */
/*****************************************************************************/
/* buffers_initialize() */
/*---------------------------------------------------------------------------*/
/* */
/* Fill input_buffer[] and power_spectrum[] with zeroes to avoid spurious */
/* readings. */
/* */
/*****************************************************************************/
static void buffers_initialize(void)
{
unsigned int i;
for ( i = 0 ; i < N ; i++ )
{
input_buffer[i] = 0;
power_spectrum[i] = 0;
}
}
/*****************************************************************************/
/* shuffle_and_read() */
/*---------------------------------------------------------------------------*/
/* */
/* Shuffle input buffer along one place. Put latest input measurement */
/* into first buffer location after dividing by 4 to limit input to range */
/* -8191 to + 8192, thus avoiding overloading the FFT. */
/* */
/*****************************************************************************/
static void shuffle_and_read(signed int input)
{
signed int i;
for ( i = N-1 ; i > 0 ; i--)
{
input_buffer[i] = input_buffer[i-1];
}
/* Put newest sample into the buffer. Limit range to -8191 to +8191 */
input_buffer[0] = ( input >> 2 );
}
/*****************************************************************************/
/* copy_input_to_complex() */
/*---------------------------------------------------------------------------*/
/* */
/* Copy from input buffer to complex structure. */
/* When the Fast Fourier Transform ( FFT ) is performed, the complex */
/* structure is overwritten with the return values. */
/* */
/*****************************************************************************/
static void copy_input_to_complex(void)
{
unsigned int i;
for ( i = 0 ; i < N; i++)
{
(y[i]).real = input_buffer[i];
(y[i]).imag = 0;
}
}
/*****************************************************************************/
/* calculate_output_power() */
/*---------------------------------------------------------------------------*/
/* */
/* Calculate output power of real and imaginary terms */
/* */
/* Magnitude = sqrt ( real * real + imaginary * imaginary) */
/* */
/* Power is proportional to the Magnitude squared, which is easier than */
/* performing a square root calculation. */
/* */
/*****************************************************************************/
static void calculate_output_power ( COMPLEX * y, unsigned int * output)
{
unsigned int i;
long temp;
for ( i = 0 ; i < N ; i++ )
{
temp = ( (long) (y[i]).real * (y[i]).real);
temp += ( (long) (y[i]).imag * (y[i]).imag );
output[i] = ( unsigned int) ( temp >> 15 );
}
}
/*****************************************************************************/
/* calculate_maximum_output_power() */
/*---------------------------------------------------------------------------*/
/* */
/* Calculate greatest output power of values of power_spectrum[1] to */
/* power_spectrum[11]. Ignore power_spectrum[0]. */
/* */
/*****************************************************************************/
static unsigned int calculate_maximum_output_power(void)
{
unsigned int i;
unsigned int result;
result = 0;
for ( i = 1 ; i < 12 ; i++ )
{
if ( power_spectrum[i] > result)
{
result = power_spectrum[i];
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -