使用内插和repmat在倍频程和Matlab中更改信号的频率 [英] Using interpolation and repmat to change frequency of signal in octave and matlab

查看:384
本文介绍了使用内插和repmat在倍频程和Matlab中更改信号的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用类似于Matlab的Octave 3.8.1,我试图使用内插和repmat来更改信号的频率(,因为这样做是如此之快(.01秒),我必须一次创建28000 + ).我可以将变量num_per_sec更改为任何整数,但是如果我尝试将其更改为2.1或带有小数的任何内容,我都会报错 错误:重塑:无法将44100x2数组重塑为92610x1数组错误:从第10行(repmat行)调用",有人针对这个或其他建议有解决方法吗?

I'm using Octave 3.8.1 which is like matlab, I'm trying to use interpolation and repmat to alter the frequency of signals (since it's so fast doing it this way (.01 seconds) and I have to create 28000+ at a time) I can change the variable num_per_sec to any whole number but if I try to change it to 2.1 or anything with a decimal in it I get an error "error: reshape: can't reshape 44100x2 array to 92610x1 array error: called from: line 10 (the repmat line)" does anyone have a work around for this or another suggestion?

注意:请注意, ya 是一个简单的测试方程式.我将没有方程式可以使用仅信号来工作,因此仅更改freq变量将不起作用.

Note: please note that ya is a simple test equation I won't have equations to work with just the signal to work with so just changing the freq variable won't work.

请参见下面的代码:

clear,clc
fs = 44100;                   % Sampling frequency
t=linspace(0,2*pi,fs);
freq=1;
ya = sin(freq*t)';  %please note that this is a simple test equation I won't have equations just the signal to work with.

num_per_sec=2 %works
%num_per_sec=2.1 %doesn't work
yb=repmat(ya,num_per_sec,1);%replicate matrix 
xxo=linspace(0,1,length(yb))'; %go from 0 to 1 sec can change speed by incr/decr 1
xxi=linspace(0,1,length(ya))'; %go from 0 to 1 sec and get total samplerate from total y value
yi_t=interp1(xxo,yb,xxi,'linear');

plot(yi_t)

推荐答案

您正在尝试使用浮点数调用repmat.显然,它不会按您的预期方式工作. repmat的预期操作是将特定维度复制整数次.

You are trying to call repmat with a floating point number. Obviously it won't work the way you intended. repmat's intended operation is to replicate a particular dimension an integer number of times.

因此,我可以建议的一件事是将信号截断不超过1的倍数,然后将其堆叠在复制信号的末尾.例如,如果要复制信号2.4次,则通常将整个信号复制两次,然后将信号长度的40%堆叠到阵列的末尾.因此,您最多要采样信号总持续时间的40%,并将其放置在复制信号的末尾.

Therefore, one thing I can suggest is to perhaps truncate the signal for the multiple which doesn't go up to 1 and stack this at the end of the replicated signal. For example, if you wanted to replicate a signal 2.4 times, you'd replicate the entire signal twice normally, then stack 40% of the length of the signal to the end of the array. Therefore, you'd sample up to 40% of the total duration of the signal and place this at the end of the replicated signal.

由于您具有采样频率,因此可以告诉您每秒应包含多少个采样.这样,找出您有多少整数倍,然后通过将该百分比的底数乘以您的采样频率来确定部分信号包含多少个采样.然后,您可以从中进行采样并将其堆叠在信号末尾.例如,以2.4的示例为例,我们将执行floor(0.4*fs)来确定需要提取的信号开始处的样本总数,以将其放置在复制信号的末尾.

Because you have the sampling frequency, this tells you how many samples per second the signal should consist. As such, figure out how many integer multiples you have, then determine how many samples the partial signal consists of by taking the floor of this percentage multiplied by your sampling frequency. You'd then sample from this and stack this at the end of the signal. For example, going with our example of 2.4, we would perform floor(0.4*fs) to determine the total number of samples from the beginning of the signal we would need to extract to place this at the end of the replicated signal.

类似这样的东西:

%// Your code
clear, clc
fs = 44100; %// Define sampling frequency                 
t=linspace(0,2*pi,fs);
freq=1;
ya = sin(freq*t)'; %// Define signal

num_per_sec=2.1; %// Define total number of times we see the signal

%// New code
%// Get total number of integer times we see the signal
num_whole = floor(num_per_sec);

%// Replicate signal
yb=repmat(ya,num_whole,1);

%// Determine how many samples the partial signal consists of
portion = floor((num_per_sec - num_whole)*fs);

%// Sample from the original signal and stack this on top of replicated signal
yb = [yb; ya(1:portion)];

%// Your code
xxo=linspace(0,1,length(yb))'; 
xxi=linspace(0,1,length(ya))'; 
yi_t=interp1(xxo,yb,xxi,'linear');

这篇关于使用内插和repmat在倍频程和Matlab中更改信号的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆