Search code examples
matlabwindowfft

error in using fftoneside


Hi I'm trying to calculate mfcc for which i'm windowing. I have seen this one post I'm getting error in fftOneSide.
my code is

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

the error i'm getting is

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

any help is appreciated thanks


Solution

  • This is a really old post, it'd be neat if someone still cared. I just provided what I believe to be the answer in the recent post below, which I arrived at after a fair bit of frustration: Undefined function 'fftOneSide' for input arguments of type 'double'.

    It should be noted here there's a call to a file, which I'm not sure if the author had originally or not. I suspect all the problems are related to a similarly named file in the sourcecode.

    If you look at my discussion in the other post, within the function definition there is a call to a method demo with a file - which isn't present if you just have the function definition, not the original file. Calling [mag1, phase1, freq1]=fftOneSide(original, fs,1), after you comment out the first line if nargin <1... and the demo routine worked fine on my machine with the code which I'll show below. Having the third argument equal to 1 guarantees that the code will run the print routines, which is important.

    In case the other thread is closed, I just want to show the output, when the input is manually defined, and I call [mag1, phase1, freq1]=fftOneSide(original, fs,1) on the properly edited method, with the inputs as in the original post shown below (notice the call is to original in fftOneSide..in the other post it was like this as well.. I believe the call was was meant to be instead for windowed, but I don't have the signals toolbox with hamming anyways):

    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);
    

    The output is as follows (source code below!)

    output from fftOneSide

    Anyways, here's the source code in case anyone wants to use this function

    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);