Error on Undefined function 'log' for input arguments of type 'cell'.
2 views (last 30 days)
Show older comments
![Capture.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/198474/Capture.jpeg)
close all;
%{Step 0: Reading the File & initializing the Time and Freq.
[x,fs]=audioread('baby-crying-sleepy.wav');
x=x(:,1);
ts=1/fs;
N=length(x);
Tmax=(N-1)*ts;
fsu=fs/(N-1);
t=(0:ts:Tmax);
f=(-fs/2:fsu:fs/2);
figure, subplot(411),plot(t,x),xlabel('Time'),title('Original Speech');
subplot(412),plot(f,fftshift(abs(fft(x)))),xlabel('Freq (Hz)'),title('Frequency Spectrum');
%%Step 1: Pre-Emphasis
a=1;
b=[1 -0.95];
y=filter(b,a,x);
subplot(413),plot(t,y),xlabel('Time'),title('Signal After High Pass Filter - Time Domain');
subplot(414),plot(f,fftshift(abs(fft(y)))),xlabel('Freq (Hz)'),title('Signal After High Pass Filter - Frequency Spectrum');
%%Step 2: Frame Blocking
frameSize=256;
frameOverlap=128;
frames=enframe(y,frameSize,frameOverlap);
NumFrames=size(frames,1);
%%Step 3: Hamming Windowing
hamm=hamming(256)';
for i=1:NumFrames
windowed(i,:)=frames(i,:).*hamm;
end
%%Step 4: FFT
%Taking only the positive values in the FFT that is the first half of the frame after being computed.
for i=1:NumFrames
fft(i,:)=abs(windowed(i,:))*(frameSize/2);
end
%%Step 5: Mel Filterbanks
%Lower Frequency = 300Hz
%Upper Frequency = fs/2
%With a total of 22 points we can create 20 filters.
Nofilters=20;
lowhigh=[300 fs/2];
%Here logarithm is of base 'e'
lh_mel=1125*(log(1+lowhigh/700));
mel=linspace(lh_mel(1),lh_mel(2),Nofilters+2);
melinhz=700*(exp(mel/1125)-1);
%Converting to frequency resolution
fres=floor(((frameSize)+1)*melinhz/fs);
%Creating the filters
for m =2:length(mel)-1
for k=1:frameSize/2
if k<fres(m-1)
H(m-1,k) = 0;
elseif (k>=fres(m-1)&&k<=fres(m))
H(m-1,k)= (k-fres(m-1))/(fres(m)-fres(m-1));
elseif (k>=fres(m)&&k<=fres(m+1))
H(m-1,k)= (fres(m+1)-k)/(fres(m+1)-fres(m));
elseif k>fres(m+1)
H(m-1,k) = 0;
end
end
end
%H contains the 20 filterbanks, we now apply it to the
%processed signal.
bankans=cell(NumFrames,Nofilters);
for i=20:1
for j=1:256
bankans{i,j}=sum(fft(:,i).* H(j,:));
end
end
%%Step 6: Neutral Log and DCT
%pkg load signal
%Here logarithm is of base '10'
logged=log(bankans);
for i=1:NumFrames
mfcc(i,:)=dct2(logged(i,:));
end
%plotting the MFCC
figure
hold on
for i=1:NumFrames
plot(lnd(i,:));
end
hold off
Can some help me fixing the error?
0 Comments
Answers (1)
Walter Roberson
on 7 Dec 2018
your code uses .* between a row vector and a column vector . That is an error in r2016a and earlier but valid in r2016b and later where it will produce a 2d array. you sum() the array which will give a row vector . You store the row vector as an entry in the 2d cell array bankans .
You then try to log the entire cell array bankans which is failing because log is not on operation for cells . You need to arrayfun the log operation which would get you a 2d cell array logged. You also need to consider using log10 as log is natural log.
once you have the log in logged you proceed to dct2(logged(i,:)) where i is a frame number . This is asking for dct2 of an entire row of a cell array each entry of which is a row vector . That will fail. What could potentially work is dct2(vertcat(logged{i,:})) which would arrange the row of row entries into a 2d array that it could potentially make sense to take dct2 of. The result would be a 2d array .
You then take the 2d array and try to store it as a row of mfcc. That will fail . But we know that a 2d result is intended because you would have used dct instead of dct2 if you expected a row result . So you will need to change mfcc to a cell array or to 3d.
After that you ignore mfcc and plot a variable that does not exist. Hard to deduce a meaning there .
It looks to me as if someone translated some python code to MATLAB and made mistakes .
0 Comments
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!