Reputation: 11
I am using MATLAB R2013.
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));
[mag1,phase1,freq1]=fftOneSide(original,fs);
Error:
Undefined function 'fftOneSide' for input arguments of type 'double'
Upvotes: 1
Views: 336
Reputation: 367
This question was raised in a previous SO question, with apparently no response until now: error in using fftoneside
Using your input for the signal, without using the hamming function (I don't have the signal toolbox on my computer), after defining the function below (I commented out the demo code at the bottom part and the first line with if nargin <1
obv!!), I ran it on Matlab2015a. It ran fine! Setting plotOpt=1
(that is calling with three arguments, the final equal to 1) provides proof:
The only thing I can think of, is that in the sourcecode it asks for a file as input - which I'm assuming you don't have. If you comment out the nargin <1
part, and the whole selfdemo at the end, see if this doesn't work as you want it to. If it doesn't, I'm completely baffled!
I've taken a look at the source code (below) for this function, which also shows proper usage. As you can see they're reading the signal and fs
values in from a file, rather than defining them as you have here. It's a bit of a pain we can't see what values they chose.
Notice the call asks for plotOpt
to be specified, though if you don't specify it, the nargin
calls at the beginning should set it to 0 for you (right?). I don't think default function declarations have changed since 2012 - but if they have, calling the function with three arguments will take care of that. Note: if don't set plotOpt=1, the plots won't proceed.
http://read.pudn.com/downloads99/sourcecode/others/404673/audioProcessing/fftOneSide.m__.htm
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 % MBio: comment 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 MBio: comment ALL this code 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);
Upvotes: 1