简体   繁体   English

如何更改 numpy.array 采样率?

[英]How to change numpy.array sample rate?

Say, I've got a signal which is one dimensional numpy array lasting for one second with sample rate equal to 16 kHz.说,我有一个信号,它是一维 numpy 数组,持续一秒钟,采样率等于 16 kHz。 How can I resample this array for instance to 1024 Hz without loosing information about "peaks" present in this signal as shown below?如何在不丢失有关此信号中存在的“峰值”的信息的情况下将此阵列重新采样为例如 1024 Hz,如下所示? I add only that intervals between "peaks" are not less, than 40 ms.我只补充说“峰值”之间的间隔不少于 40 毫秒。

UPDATE更新

I've found an interesting approach based on scipy.signal.resample function, but the problem with this function is that it return absolute value for each y value of the given vector.我发现了一种基于scipy.signal.resample函数的有趣方法,但该函数的问题在于它返回给定向量的每个y值的绝对值。

在此处输入图片说明

If you can use scipy , I suggest scipy.interpolate.interp1d .如果您可以使用scipy ,我建议使用scipy.interpolate.interp1d

However also numpy has a 1D interpolation in numpy.interp , so that might just be what you want.然而numpynumpy.interp有一个一维插值,所以这可能正是你想要的。

For what you are asking (not loosing the peaks), that is a bit of an ill posed problem, that can't be solved by a simple and naîve interpolation.对于您所要求的(不丢失峰值),这是一个不恰当的问题,无法通过简单而幼稚的插值来解决。

My 2 cents solution: Since you are downsampling, and you know the downsampling ratio, you can just extend the peaks so to not lose them after downsampling, ie so that the peak duration in the downsampled signal is at least one sample, that is, is at least ceil(SR1/SR2) samples before downsampling, where SR1 is the original sample rate, and SR2 is the downsampled rate..我的 2 美分解决方案:由于您正在下采样,并且您知道下采样比率,因此您可以扩展峰值以便在下采样后不会丢失它们,即下采样信号中的峰值持续时间至少是一个样本,即,至少是下采样前的ceil(SR1/SR2)样本,其中SR1是原始采样率, SR2是下采样率。

Plain resampling will always loose some information.简单的重采样总是会丢失一些信息。 If the height of the peaks is important, but not so much width, you could just take 1 every 16 samples (given that the peaks are at least 16 samples wide at 16kHz).如果峰值的高度很重要,但宽度不那么重要,您可以每 16 个样本取 1 个(假设峰值在 16kHz 下至少有 16 个样本宽)。

If you have few peaks, you could consider a far better algorithm: Just record the time instant and delta, for each change.如果您的峰值很少,您可以考虑一个更好的算法:只需记录每次更改的时间瞬间和增量。 In your diagram (estimated):在您的图表中(估计):

21050 +2000
21060 -2000
24030 -500
24040 +500
26030 +1000
26040 -1000

This will surely occupy much less than sampling.这肯定会比采样少得多。 In just 6 values (which could be stored in binary) I compressed your data.我只用 6 个值(可以以二进制形式存储)压缩了您的数据。

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

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