代码之家  ›  专栏  ›  技术社区  ›  Tyler Hilbert

Matlab:信号处理中矩阵的正确对半

  •  1
  • Tyler Hilbert  · 技术社区  · 8 年前

    我正在从事信号处理Matlab项目。我正在执行FFT并将矩阵切成两半,以仅获得单侧频谱。我使用了以下几行代码,得到了错误“警告:冒号运算符用作索引时需要整数操作数 ":

    amp = abs(y).^2/n;    % amplitude of the DFT
    amp = amp(1:end/2);
    

    为了消除警告,我尝试手动将其舍入,但当我这样做时,我得到了错误“error using round 输入参数不足。"

    amp = abs(y).^2/n;    % amplitude of the DFT
    amp = amp(1:round(end/2));
    

    我想知道的是,对于单面光谱,将矩阵一分为二的正确方法是什么?作为旁注,这是整个代码:

    clear all;
    
    % Read Audio
    fs = 44100;         % sample frequency (Hz)
    full = audioread('song.wav');
    
    % Remove leading 0's and select range
    for i = 1:fs
        if full(i) ~= 0
            crop = i;
            break
        end
    end
    full = full(crop:end);
    
    startTime = 1;
    endTime = 5;
    
    % Play song
    tic
    initialTime = toc;
    player = audioplayer(full(fs*startTime:fs*endTime), fs);
    player.play();
    
    % Perform fft and get frequencies (hopefully in realish time with audio)
    windowSize = fs/16;
    for i = windowSize/2+1+fs*(startTime-1) : fs/32 : fs*endTime
        beginningChunk = round(i-windowSize/2);
        endChunk = round(i+windowSize/2);
    
        x = full(beginningChunk:endChunk);
        y = fft(x);
        n = length(x);     % number of samples in chunk
        amp = abs(y).^2/n;    % amplitude of the DFT
        amp = amp(1:round(end/2));
        f = (0:n-1)*(fs/n);     % frequency range
        f = f(1:round(end/2));
        while initialTime+i/fs > toc
            pause(.0001);
        end
        figure(1);
        plot(f,amp);
        axis([0 10000 0 5]);
        xlabel('Frequency');
        ylabel('amplitude');
    end
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Cris Luengo    8 年前

    如果 amp(1:round(end/2)) 对您不起作用(似乎对R2017a起作用),然后尝试 amp(1:round(length(amp)/2)) 相反(或更好,使用 floor .)

    请注意 end 在这种情况下 numel(amp) 因为您使用线性索引(使用一个值进行索引)。因为 amp 是一个向量, length 都是一样的。

    一般情况下, A(end,end) ,第一个端点等效于 size(A,1) 第二个是 size(A,2) .


    出现错误的第二个原因是,如果您有一个名为 round . 类型 which round 在MATLAB命令提示符上查找键入时调用的内容 round(1) .