使用fftoneside时出错

时间:2013-10-23 07:43:46

标签: matlab window fft

嗨,我正在尝试计算我正在开窗的mfcc。我看过这篇文章,我在fftOneSide中遇到错误 我的代码是

    waveFile='test_preEmphasis.wav';
    [y, fs]=wavread(waveFile);

    n=512;
    t=(1:n)'/fs;
    startIndex=30418;
    endIndex=startIndex+n-1;

    original=y(startIndex:endIndex);
    windowed=original.*hamming(n);
    [mag1, phase1, freq1]=fftOneSide(original, fs);
    [mag2, phase2, freq2]=fftOneSide(windowed, fs);

    subplot(3,2,1); plot(original); grid on; axis([-inf inf -1 1]); 
    title('Original signal');
    subplot(3,2,2); plot(windowed); grid on; axis([-inf inf -1 1]);  
    title('Windowedsignal');
    subplot(3,2,3); plot(freq1, mag1); grid on; 
    title('Energy spectrum (linear scale)');
    subplot(3,2,4); plot(freq2, mag2); grid on; 
    title('Energy spectrum (linear scale)');
    subplot(3,2,5); plot(freq1, 20*log(mag1)); grid on; 
    axis([-inf inf -80 120]); title('Energy spectrum (db)');
    subplot(3,2,6); plot(freq2, 20*log(mag2)); grid on; axis([-inf inf -80 120]);  
    title('Energy spectrum (db)');

我得到的错误是

    ??? Undefined function or method 'fftOneSide' for input arguments of type 'double'.

任何帮助表示赞赏 感谢

1 个答案:

答案 0 :(得分:2)

这是一个非常古老的帖子,如果有人仍然关心,它会很整洁。我刚刚在下面的帖子中提供了我认为是答案的答案,这是我在遇到一些挫折之后得出的:Undefined function 'fftOneSide' for input arguments of type 'double'

这里应该注意的是对文件的调用,我不确定作者是否原来是这样的。我怀疑所有问题都与源代码中类似命名的文件有关。

如果你看看我在其他帖子中的讨论,在函数定义中调用带有文件的方法demo - 如果你只有函数定义则不会出现,而不是原始文件。在您注释掉第一行[mag1, phase1, freq1]=fftOneSide(original, fs,1)并且if nargin <1...例程在我的计算机上正常工作时,使用我将在下面显示的代码调用demo。让第三个参数等于1可以保证代码运行打印例程,这很重要。

如果另一个线程关闭,我只想显示输出,当手动定义输入时,我在正确编辑的方法上调用[mag1, phase1, freq1]=fftOneSide(original, fs,1),输入如下面显示的原始帖子中所示(请注意,original中的fftOneSide来电,在另一篇帖子中也是如此。我相信这个电话本来是为了windowed,但是我无论如何都没有hamming的信号工具箱:

fs=8000;
t=(1:512)'/fs; %'// <-- prevents string markdown
f=306.396;
original=sin(2*pi*f*t)+0.2*randn(length(t),1);
% windowed=original.*hamming(length(t)); % defined in other post
[mag1,phase1,freq1]=fftOneSide(original,fs); % I call fftOneSide(original,fs,1);

输出如下(源代码如下!)

output from fftOneSide

无论如何,如果有人想要使用这个功能,这里是源代码

function [magSpec, phaseSpec, freq, powerSpecInDb]=fftOneSide(signal, fs, plotOpt) 
% fftOneSide: One-sided FFT for real signals 
%   Usage: [magSpec, phaseSpec, freq, powerSpecInDb]=fftOneSide(signal, fs) 
% 
%   For example: 
%       [y, fs]=wavread('welcome.wav'); 
%       frameSize=512; 
%       startIndex=2047; 
%       signal=y(startIndex:startIndex+frameSize+1); 
%       signal=signal.*hamming(length(signal)); 
%       plotOpt=1; 
%       [magSpec, phaseSpec, freq, powerSpecInDb]=fftOneSide(signal, fs, plotOpt); 

%   Roger Jang, 20060411, 20070506 

if nargin<1, selfdemo; return; end %=== (MathBio: Comment this out!)
if nargin<2, fs=1; end 
if nargin<3, plotOpt=0; end 

N = length(signal);         % Signal length 
freqStep = fs/N;            % Frequency resolution 
time = (0:N-1)/fs;          % Time vector 
z = fft(signal);            % Spectrum 
freq = freqStep*(0:N/2);        % Frequency vector 
z = z(1:length(freq));          % One side 
z(2:end-1)=2*z(2:end-1);        % Assuming N is even, symmetric data is multiplied by 2
magSpec=abs(z);             % Magnitude spectrum 
phaseSpec=unwrap(angle(z));     % Phase spectrum 
powerSpecInDb=20*log(magSpec+realmin);  % Power in db 

if plotOpt 
    % ====== Plot time-domain signals 
    subplot(3,1,1); 
    plot(time, signal, '.-'); 
    title(sprintf('Input signals (fs=%d)', fs)); 
    xlabel('Time (seconds)'); ylabel('Amplitude'); axis tight 
    % ====== Plot spectral power 
    subplot(3,1,2); 
    plot(freq, powerSpecInDb, '.-'); grid on 
    title('Power spectrum'); 
    xlabel('Frequency (Hz)'); ylabel('Power (db)'); axis tight 
    % ====== Plot phase 
    subplot(3,1,3); 
    plot(freq, phaseSpec, '.-'); grid on 
    title('Phase'); 
    xlabel('Frequency (Hz)'); ylabel('Phase (Radian)'); axis tight 
    end 

% ====== Self demo (MathBio: Comment all of this out! )
function selfdemo 
[y, fs]=wavread('welcome.wav'); 
frameSize=512; 
startIndex=2047; 
signal=y(startIndex:startIndex+frameSize+1); 
signal=signal.*hamming(length(signal)); 
[magSpec, phaseSpec, freq, powerSpecInDb]=feval(mfilename, signal, fs, 1);