简体   繁体   English

如何实现Matlab的“费率转换”(在C#中)

[英]How to implement Matlab's “rate transition” (in C#)

I have a signal that is sampled at the rate of 10 000 Hz, and I need to resample it down to 4 000 Hz. 我有一个以10000 Hz的频率采样的信号,我需要将其重新采样到4000 Hz。 In MATLAB I use a simulink model with three simple blocks: "From Workspace" with specified sample time (0.0001 s) -> "Rate Transition" with specified output sample time 0.00025 s -> "To Workspace" to save the output data. 在MATLAB中,我使用具有三个简单块的simulink模型:具有指定采样时间(0.0001 s)的“从工作区”->具有指定输出采样时间0.00025 s的“速率转换”->“至工作区”以保存输出数据。 (One the rate transition block i see "No Op"). (一个速率转换块,我看到“无操作”)。

I thought that the same thing may be done using Matlab functions such as "interp1" to interpolate data but no luck. 我以为可以使用Matlab函数(例如“ interp1”)插值数据来完成相同的操作,但是没有运气。 I have tried everything, and still I don't know how to implement the same functionality that "rate transition" has. 我已经尝试了所有方法,但仍然不知道如何实现“费率过渡”所具有的相同功能。

I need to write this data resampling in C#, and my question is: what is this simulink's underlying algotithm what ports data from one sampling frequency to another? 我需要用C#编写数据重采样,我的问题是:这个simulink的底层算法是什么,它将数据从一个采样频率移植到另一个采样频率? Or how else can I get the effect i need? 或者我还能如何获得所需的效果?

Thanks, KP 谢谢,KP

You can interpolate in this way: 您可以通过以下方式进行插值:

% Sampling frequency
Fs1 = 10e3;
Fs2 = 4e3;

% Load your data
y = load('yourdata'); %y=sin(0:1/Fs1:1);
Ttime = (length(y)-1)*(1/Fs1);

% X-Axis
x  = 0:1/Fs1:Ttime;
xi = 0:1/Fs2:Ttime;

% Interpolate
method = 'cubic';
yi = interp1(x,y,xi,method);

% Plot
figure
hold on
scatter(x,y,'b');
scatter(xi,yi,'r');
hold off
legend('Fs=10k','Fs=4k')

The main step is 'interp1' that performs one-dimension interpolation. 主要步骤是执行一维插值的“ interp1”。

It seems that the Rate Transition block does not do any interpolation. 速率转换块似乎没有进行任何插值。 It behaves like a zero-order hold when the sampling frequency is higher in the input than in the output. 当输入中的采样频率高于输出中的采样频率时,其行为类似于零阶保持。 Thus, you could try this: 因此,您可以尝试以下操作:

% Sampling frequency
Fs1 = 10e3;
Fs2 = 4e3;

% Load your data
y = load('yourdata'); %y=sin(0:1/Fs1:1);
Ttime = (length(y)-1)*(1/Fs1);

% X-Axis
x  = 0:1/Fs1:Ttime;
xi = 0:1/Fs2:Ttime;

% Zero-order hold
yi = zeros(length(xi),1);
jj = 1;
xi(1) = x(1);
for ii=2:length(y)
    % Update value
    if (x(ii)>=xi(jj)),
        yi(jj) = y(ii-1);
        jj = jj+1;
    end
end

% Plot
figure
hold on
scatter(x,y,'b');
scatter(xi,yi,'r');
hold off
legend('Fs=10k','Fs=4k')

The only modification with the previous code is to estimate the yi (y-axis interpolated) from a zero-order hold. 先前代码的唯一修改是从零阶保持估计yi(内插y轴)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM