【问题标题】:Using interpolation and repmat to change frequency of signal in octave and matlab在八度和matlab中使用插值和repmat改变信号的频率
【发布时间】:2015-09-08 12:46:11
【问题描述】:

我正在使用类似于 matlab 的 Octave 3.8.1,我正在尝试使用插值和 repmat 来改变信号的频率(因为这样做非常快(0.01 秒),我必须一次创建 28000+ 个我可以将变量 num_per_sec 更改为任何整数,但如果我尝试将其更改为 2.1 或其中包含小数的任何内容,我会收到错误 “错误:重塑:无法将 44100x2 数组重塑为 92610x1 数组错误:调用自:第 10 行(repmat 行)”有没有人可以解决这个或另一个建议?

注意:请注意 ya 是一个简单的测试方程 我没有方程可以仅处理信号,因此仅更改频率变量是行不通的。

见下面的代码:

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)

【问题讨论】:

    标签: arrays matlab signal-processing octave


    【解决方案1】:

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

    因此,我可以建议的一件事可能是截断不达到 1 的倍数的信号,并将其堆叠在复制信号的末尾。例如,如果你想复制一个信号 2.4 次,你通常会复制整个信号两次,然后将 40% 的信号长度堆叠到数组的末尾。因此,您最多需要对信号总持续时间的 40% 进行采样,并将其放在复制信号的末尾。

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

    类似这样的:

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

    【讨论】:

    • 如果信号比 fs 长,请确保将部分行更改为 portion = floor((num_per_sec - num_whole)*length(ya));
    猜你喜欢
    • 1970-01-01
    • 2016-04-25
    • 2020-03-25
    • 1970-01-01
    • 2015-02-27
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多