简体   繁体   English

使用内插和repmat在倍频程和Matlab中更改信号的频率

[英]Using interpolation and repmat to change frequency of signal in octave and matlab

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? 我正在使用像Matlab一样的Octave 3.8.1,我试图使用内插和repmat来改变信号的频率( 因为这样做是如此之快(.01秒),我必须在28000以上创建)一次我可以将变量num_per_sec更改为任何整数,但是如果尝试将其更改为2.1或带小数的任何内容,则会收到错误消息 “错误:重塑:无法将44100x2数组重塑为92610x1数组错误:称为来自:第10行(repmat行)”,有人针对此建议或其他建议有解决方法吗?

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. 注意:请注意, ya是一个简单的测试方程式, 我没有方程式可以与仅适用于信号的方程式一起使用,因此仅更改freq变量将不起作用。

see code below: 参见下面的代码:

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)

You are trying to call repmat with a floating point number. 您正在尝试使用浮点数调用repmat Obviously it won't work the way you intended. 显然,它不会按照您的预期工作。 repmat 's intended operation is to replicate a particular dimension an integer number of times . repmat的预期操作是将特定维度复制整数次

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. 因此,我可以建议的一件事是将信号截断为不大于1的倍数,并将其堆叠在复制信号的末尾。 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. 例如,如果要复制信号2.4次,则通常将整个信号复制两次,然后将信号长度的40%堆叠到数组的末尾。 Therefore, you'd sample up to 40% of the total duration of the signal and place this at the end of the replicated signal. 因此,您最多采样信号总持续时间的40%,并将其放置在复制信号的末尾。

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. 例如,以2.4的示例为例,我们将执行floor(0.4*fs)以确定从信号开始处需要提取的样本总数,以将其放置在复制信号的末端。

Something like this: 像这样:

%// 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');

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

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