如何使用循环缓冲区将信号移动 90 度
How can I shift a signal by 90 degrees using a circular buffer
我正在尝试通过在 Control Studio V6.02 中编写 C 代码来对 DSP (TMSF28335) 进行编程。
在这个项目中,我需要对传感器测量的交流信号进行 90 度相位偏移。有人建议我使用循环缓冲区来进行这种相移。但不幸的是,我不是很熟悉如何用C语言编写循环缓冲区。根据概念,我知道缓冲区的 "head" 应该是输入信号(测量的交流信号), "tail" 是用作循环缓冲区输出信号的移位输入信号.
系统的采样时间设置为3.84599989e-5(s),一个周期为0.02(s)(50Hz)。因此,一个周期的 1/4 构成 (0.02/4)/3.84599989e-5=130 个样本。换句话说,我需要延迟 130 个样本。
如果你能告诉我如何在 C 中为我的控制器编写循环缓冲区,我将不胜感激,因此我可以进行相位延迟。
您需要的是称为延迟线的循环缓冲区的特殊实现。这是一个简单的(快速而肮脏,有点天真)实现。
请注意,只有当输入频率恒定时,这才能为您提供固定相移。
typedef short Sample; // change to whatever your sample data is...
#define DELAY (130)
struct DelayLine // <- make sure you initialize the entire struct
{ // with zeroes before use !
Sample buffer[DELAY + 1];
int next;
};
Sample tick(DelayLine* effect, Sample sampleIn)
{
// call for each sample received, returns the signal after delay
int nextOut = effect->next + DELAY; // note that this delay could be anything
// shorter than the buffer size.
if (nextOut >= DELAY + 1) // <-- but not this one!!!
nextOut -= DELAY + 1;
effect->buffer[effect->next] = sampleIn;
if (++(effect->next) >= DELAY + 1)
effect->next = 0;
return effect->buffer[nextOut];
}
我正在尝试通过在 Control Studio V6.02 中编写 C 代码来对 DSP (TMSF28335) 进行编程。
在这个项目中,我需要对传感器测量的交流信号进行 90 度相位偏移。有人建议我使用循环缓冲区来进行这种相移。但不幸的是,我不是很熟悉如何用C语言编写循环缓冲区。根据概念,我知道缓冲区的 "head" 应该是输入信号(测量的交流信号), "tail" 是用作循环缓冲区输出信号的移位输入信号.
系统的采样时间设置为3.84599989e-5(s),一个周期为0.02(s)(50Hz)。因此,一个周期的 1/4 构成 (0.02/4)/3.84599989e-5=130 个样本。换句话说,我需要延迟 130 个样本。
如果你能告诉我如何在 C 中为我的控制器编写循环缓冲区,我将不胜感激,因此我可以进行相位延迟。
您需要的是称为延迟线的循环缓冲区的特殊实现。这是一个简单的(快速而肮脏,有点天真)实现。
请注意,只有当输入频率恒定时,这才能为您提供固定相移。
typedef short Sample; // change to whatever your sample data is...
#define DELAY (130)
struct DelayLine // <- make sure you initialize the entire struct
{ // with zeroes before use !
Sample buffer[DELAY + 1];
int next;
};
Sample tick(DelayLine* effect, Sample sampleIn)
{
// call for each sample received, returns the signal after delay
int nextOut = effect->next + DELAY; // note that this delay could be anything
// shorter than the buffer size.
if (nextOut >= DELAY + 1) // <-- but not this one!!!
nextOut -= DELAY + 1;
effect->buffer[effect->next] = sampleIn;
if (++(effect->next) >= DELAY + 1)
effect->next = 0;
return effect->buffer[nextOut];
}