📄 singenerator.cpp
字号:
// SinGenerator.cpp: implementation of the CSinGenerator class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PSignalGenerate.h"
#include "SinGenerator.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/* this is a matlab language source code for Oscillator
%--------------------------------------------------
% 1/ Test the Discrete Oscillator
%--------------------------------------------------
% recursive resonator filter:
% y(n) = 2cos(wo).y(n-1) -y(n-2)
%
% initiales conditions
% y(-1) = 0 , y(-2) = -Asin(wo)
%
% wo = 2*pi*fo/fs
% fo = Output frequency must be < fs/2
% fs = sampling frequency
%--------------------------------------------------
clear;
fo = 500;
fs = 11025;
N = 100; % sequence lenth
wo = 2*pi*fo/fs;
%------------------------
% Set initial conditions
y(1)= -sin(wo); % for y(-2)
y(2)= 0; % for y(-1)
%---------------------------
% compute the sequence y(n)
for J = 3:N,
y(J) = 2*cos(wo)*y(J-1)-y(J-2);
end
first samples
-0.2811 0 0.2811 0.5396 0.7545 0.9086 0.9894
*/
//#define SHORT
CSinGenerator::CSinGenerator()
{
double pi = 4.0*atan(1.0);
fo = 300;
fs = (double)m_WaveOutSampleRate;
wo = 2*pi*fo/fs;
Ampl = 10000;
m_2cosWo =2*cos(wo);
SetInitialConditions();
m_Toggle = 0;
ComputeSamples(NULL);
m_Toggle = 1;
ComputeSamples(NULL);
}
CSinGenerator::~CSinGenerator()
{
}
void CSinGenerator::SetInitialConditions()
{
OutputBufferR[1][m_NbMaxSamples-2]= (double) (Ampl * -sin(wo));
OutputBufferR[1][m_NbMaxSamples-1]= 0;
}
void CSinGenerator::ComputeSamples(SHORT *unused)
{
if (m_Toggle)
{
OutputBufferR[1][0] = (double)( m_2cosWo*OutputBufferR[0][m_NbMaxSamples-1]-OutputBufferR[0][m_NbMaxSamples-2]);
OutputBuffer[1][0] = (SHORT)OutputBufferR[1][0];
OutputBufferR[1][1] = (double)( m_2cosWo*OutputBufferR[1][0]-OutputBufferR[0][m_NbMaxSamples-1]);
OutputBuffer[1][1] = (SHORT)OutputBufferR[1][1];
for (int i = 2; i< m_NbMaxSamples; i++)
{
OutputBufferR[1][i] = (double)( m_2cosWo*OutputBufferR[1][i-1]-OutputBufferR[1][i-2]);
OutputBuffer[1][i] = (SHORT)OutputBufferR[1][i];
}
}
else
{
OutputBufferR[0][0] = (double)( m_2cosWo*OutputBufferR[1][m_NbMaxSamples-1]-OutputBufferR[1][m_NbMaxSamples-2]);
OutputBuffer[0][0] = (SHORT)OutputBufferR[0][0];
OutputBufferR[0][1] = (double)( m_2cosWo*OutputBufferR[0][0]-OutputBufferR[1][m_NbMaxSamples-1]);
OutputBuffer[0][1] = (SHORT)OutputBufferR[0][1];
for (int i = 2; i< m_NbMaxSamples; i++)
{
OutputBufferR[0][i] = (double)( m_2cosWo*OutputBufferR[0][i-1]-OutputBufferR[0][i-2]);
OutputBuffer[0][i] = (SHORT)OutputBufferR[0][i];
}
}
}
void CSinGenerator::SetSinParametres(SHORT A, double freq)
{
Ampl = A;
fo= freq;
}
void CSinGenerator::Restart()
{
double pi = 4.0*atan(1.0);
CloseOutput();
wo = 2*pi*fo/fs;
m_2cosWo =2*cos(wo);
SetInitialConditions();
m_Toggle = 0;
ComputeSamples(NULL);
m_Toggle = 1;
ComputeSamples(NULL);
OpenOutput();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -