有没有办法在正弦方程中设置一个像 "x" 这样的 "unknown" 变量,然后再改变它的值?
is there a way to set an "unknown" variable like "x" inside a sine-equation, and change its value afterwards?
我想用 C++ 为我的基于微控制器的合成器编写一个音频代码,它应该允许我使用傅立叶级数方程生成一个采样方波信号。
我的一般问题是:有没有办法在正弦方程中设置像“x”这样的“未知”变量,然后再更改它的值?
这是什么意思:
如果您看一下我到目前为止编写的代码,您会看到以下内容:
void SquareWave(int mHarmonics){
char x;
for(int k = 0; k <= mHarmonics; k++){
mFourier += 1/((2*k)+1)*sin(((2*k)+1)*2*M_PI*x/SAMPLES_TOTAL);
}
for(x = (int)0; x < SAMPLES_TOTAL; x++){
mWave[x] = mFourier;
}
}
在第一个 for 循环中,mFourier 正在对取决于谐波“mHarmonics”数量的加权正弦信号求和。所以我键盘上的一个音符应该是自动设置谐波频谱。
在这个等式中,我将 x 设置为一个字符,现在我们进入问题的中心,因为我想将 x 设置为一个“未知”变量,它有一个我想在之后设置的值,如果 x 是一个整数它会有一些标准值,例如 0,这会使整个等式不正确。
在底部循环中,我想在数组 mWave 中写入这个傅里叶级数和,这将是结果输出。是否有可能将总和提供给 mWave[x],其中 x 首先是正弦信号内的“未知”乘数,然后在第二个循环内更改其值?
抱歉,如果这是一个愚蠢的问题,我对 C++ 的经验不多,但我尝试通过犯这些愚蠢的错误来学习它!
干杯
@Useless 告诉过你该怎么做,但我会试着把它拼出来给你。
我会这样做:
#include <vector>
/**
* Perform a rectangular window in the frequency domain of a time domain square
* wave. This should be a sync impulse response.
*
* @param x The time domain sample within the period of the signal.
* @param harmonic_count The number of harmonics to aggregate in the result.
* @param sample_count The number of samples across the square wave period.
*
* @return double The time domain result of the combined harmonics at point x.
*/
double box_car(unsigned int x,
unsigned int harmonic_count,
unsigned int sample_count)
{
double mFourier = 0.0;
for (int k = 0; k <= harmonic_count; k++)
{
mFourier += 1.0 / ((2 * k) + 1) * sin(((2 * k) + 1) * 2.0 * M_PI * x / sample_count);
}
return mFourier;
}
/**
* Calculate the suqare wave samples across the time domain where the samples
* are filtered to only include the harmonic_count.
*
* @param harmonic_count The number of harmonics to aggregate in the result.
* @param sample_count The number of samples across the square wave period.
*
* @return std::vector<double>
*/
std::vector<double> box_car_samples(unsigned int harmonic_count,
unsigned int sample_count)
{
std::vector<double> square_wave;
for (unsigned int x = 0; x < sample_count; x++)
{
double sample = box_car(x, harmonic_count, sample_count);
square_wave.push_back(sample);
}
return square_wave;
}
因此 mWave[x] 返回为 std::vector 双精度数(浮点数)。
如前所述,函数 box_car_samples() 是 f(k, x)。
因此,由于我无法在 Arduino IDE 中使用向量,所以我尝试了以下解决方案:
...
void ComputeBandlimitedSquareWave(int mHarmonics){
for(int i = 0; i < sample_count; i++){
mWavetable[i] = ComputeFourierSeriesSquare(x);
if (x < sample_count) x++;
}
}
float ComputeFourierSeriesSquare(int x){
for(int k = 0; k <= mHarmonics; k++){
mFourier += 1/((2*k)+1)*sin(((2*k)+1)*2*M_PI*x/sample_count);
return mFourier;
}
}
...
一分钟前我还以为一定是对的,但我的显示器证明我错了...
起初听起来像是完全混乱的信号总和,但大约 2 秒后,真正的特征方波声音传了过来。我试着弄清楚我在监督什么,如果我能把最后一部分从我的扬声器中分离出来,我会让你们更新,因为它实际上有一个非常不错的声音。只有开始的凌乱叠加让我现在很绝望...
我想用 C++ 为我的基于微控制器的合成器编写一个音频代码,它应该允许我使用傅立叶级数方程生成一个采样方波信号。
我的一般问题是:有没有办法在正弦方程中设置像“x”这样的“未知”变量,然后再更改它的值?
这是什么意思: 如果您看一下我到目前为止编写的代码,您会看到以下内容:
void SquareWave(int mHarmonics){
char x;
for(int k = 0; k <= mHarmonics; k++){
mFourier += 1/((2*k)+1)*sin(((2*k)+1)*2*M_PI*x/SAMPLES_TOTAL);
}
for(x = (int)0; x < SAMPLES_TOTAL; x++){
mWave[x] = mFourier;
}
}
在第一个 for 循环中,mFourier 正在对取决于谐波“mHarmonics”数量的加权正弦信号求和。所以我键盘上的一个音符应该是自动设置谐波频谱。 在这个等式中,我将 x 设置为一个字符,现在我们进入问题的中心,因为我想将 x 设置为一个“未知”变量,它有一个我想在之后设置的值,如果 x 是一个整数它会有一些标准值,例如 0,这会使整个等式不正确。
在底部循环中,我想在数组 mWave 中写入这个傅里叶级数和,这将是结果输出。是否有可能将总和提供给 mWave[x],其中 x 首先是正弦信号内的“未知”乘数,然后在第二个循环内更改其值?
抱歉,如果这是一个愚蠢的问题,我对 C++ 的经验不多,但我尝试通过犯这些愚蠢的错误来学习它!
干杯
@Useless 告诉过你该怎么做,但我会试着把它拼出来给你。
我会这样做:
#include <vector>
/**
* Perform a rectangular window in the frequency domain of a time domain square
* wave. This should be a sync impulse response.
*
* @param x The time domain sample within the period of the signal.
* @param harmonic_count The number of harmonics to aggregate in the result.
* @param sample_count The number of samples across the square wave period.
*
* @return double The time domain result of the combined harmonics at point x.
*/
double box_car(unsigned int x,
unsigned int harmonic_count,
unsigned int sample_count)
{
double mFourier = 0.0;
for (int k = 0; k <= harmonic_count; k++)
{
mFourier += 1.0 / ((2 * k) + 1) * sin(((2 * k) + 1) * 2.0 * M_PI * x / sample_count);
}
return mFourier;
}
/**
* Calculate the suqare wave samples across the time domain where the samples
* are filtered to only include the harmonic_count.
*
* @param harmonic_count The number of harmonics to aggregate in the result.
* @param sample_count The number of samples across the square wave period.
*
* @return std::vector<double>
*/
std::vector<double> box_car_samples(unsigned int harmonic_count,
unsigned int sample_count)
{
std::vector<double> square_wave;
for (unsigned int x = 0; x < sample_count; x++)
{
double sample = box_car(x, harmonic_count, sample_count);
square_wave.push_back(sample);
}
return square_wave;
}
因此 mWave[x] 返回为 std::vector 双精度数(浮点数)。
如前所述,函数 box_car_samples() 是 f(k, x)。
因此,由于我无法在 Arduino IDE 中使用向量,所以我尝试了以下解决方案:
...
void ComputeBandlimitedSquareWave(int mHarmonics){
for(int i = 0; i < sample_count; i++){
mWavetable[i] = ComputeFourierSeriesSquare(x);
if (x < sample_count) x++;
}
}
float ComputeFourierSeriesSquare(int x){
for(int k = 0; k <= mHarmonics; k++){
mFourier += 1/((2*k)+1)*sin(((2*k)+1)*2*M_PI*x/sample_count);
return mFourier;
}
}
...
一分钟前我还以为一定是对的,但我的显示器证明我错了... 起初听起来像是完全混乱的信号总和,但大约 2 秒后,真正的特征方波声音传了过来。我试着弄清楚我在监督什么,如果我能把最后一部分从我的扬声器中分离出来,我会让你们更新,因为它实际上有一个非常不错的声音。只有开始的凌乱叠加让我现在很绝望...