Cedrick Baker
Cedrick Baker

Reputation: 61

Issues with arrays and matlab coder

I've been working on creating c code with Matlab-coder. I have a function called melfunction that gives an error for line 20 of the code below.

function [ c ] = melfunction( x )

bank=melbankm(24,256,8000,0,0.4,'t');  
bank=full(bank); %full() convert sparse matrix to full matrix  
bank=bank/max(bank(:));

w=1+6*sin(pi*[1:12]./12);
w=w/max(w); 
xx=double(x);  
xx=filter([1-0.9375],1,xx); 
xx=enframe(xx,256,80)
p = zeros(1,256); < --------------------- SOLUTION CHANGE TO p = zeros(256)
m = zeros(1,12);  < --------------------- SOLUTION CHANGE TO p = zeros(12)

for i=1:size(xx,1)  
  y=xx(i,:);  
  s=y'.*hamming(256);  
  t=abs(fft(s));
  t=t.^2; 
  p(i,:) = t; < --------------------------- ERROR HERE
  c1=dctcoef*log(bank*t(1:129));  
  c2=c1.*w';  
  m(i,:)=c2;  
end 

error message looks like this

Error using melfunction (line 20) Index exceeds array dimensions. Index value 2 exceeds valid range [1-1] of array p.

Error in TESTINPUTS (line 2) d0=melfunction(x)

If I come up with an answer I will post it when I find one.

Upvotes: 0

Views: 66

Answers (1)

Cedrick Baker
Cedrick Baker

Reputation: 61

Ok so I messed with it a bit and found that I believe I did not know how to correctly initialize p and m . In an attempt to add these lines of code for another part of Matlab-Coder I initialized the p and m variables incorrectly. This is the original initialization.

p = zeros(1,256); m = zeros(1,12);

this is what I changed it too.

p = zeros(256);
m = zeros(12);

Upvotes: 1

Related Questions