0

I am sorry If I say some thing silly.Please forgive me: I am trying to convert Matlab code(given below) to VHDL code,using HDL coder.It contains a function called sum.But when I try to convert the code it gives me error :

code generation only supports SumModes 'SpecifyPrecision' and 'KeepLSB' for 'SUM' when the size of the inputs can vary at run-time.

But the thing is I have never used functions before.Can any one please help me on it.How should change my code to convert it to VHDL.It would be really nice!

function y = fcn(n,y1,y2)
n=10;
x1c=zeros(2*n-1,1);
for i=1:2*n-1
    if(i>n)
        j1=1;
        k1=2*n-i;
        j2=i-n+1;
        k2=n;
    else
        j1=n-i+1;
        k1=n;
        j2=1;
        k2=i;
    end
    x1c(i)=sum((y1(j1:k1)).*y2(j2:k2));
end
x1c=flipud(x1c).';
y=x1c;

This is cross correlation of y1 and y2. y1 and y2 are two vectors of same length and n is length of y1. I am really stuck please help! Thanks in advance!

toolic
  • 57,801
  • 17
  • 75
  • 117
Haider
  • 13
  • 4
  • @BrianDrummond It just flips the elements in up/down direction. – Haider Jan 12 '16 at 15:28
  • @BrianDrummond Kindly would be nice if you have a look into it please. – Haider Jan 12 '16 at 15:44
  • 1
    My experience of HDL Coder is that you're probably better off learning VHDL and translating this code by hand. What you've shown is quite a simple algorithm. –  Jan 12 '16 at 16:53
  • The warning tells you that the vectors in your sum change in size (from i=1: y1(1:19) to i=10: y1(1:10)). If you change the sum, so that it always has vectors of the same length (i.e. 1:19), the compiler should work. You can do this with creating temp vectors and zero padding them. But as already pointed out by @BrianDrummond, you would be better off learning VHDL. – dieli Jan 13 '16 at 11:00
  • @dieli Thanks for the comment. Actually my requirement is to generate vhdl code using hdl coder. Would be nice if you bit explain how I can sue temp vectors in my code please.I will really appreciate your time or SumModes 'SpecifyPrecision' and 'KeepLSB' .It would be really nice.Thanks – Haider Jan 13 '16 at 14:17

1 Answers1

0

@Haider: Take a look at

    n=10;
    y1 = 1:n;
    y2 = n:-1:1;
    x1c=zeros(1,2*n-1);
    for i=1:2*n-1
        y1_temp = zeros(1,2*n-1);
        y2_temp = zeros(1,2*n-1);
        if(i>n)
            j1=1;
            k1=2*n-i;
            j2=i-n+1;
            k2=n;
        else
            j1=n-i+1;
            k1=n;
            j2=1;
            k2=i;
        end
        y1_temp(j1:k1) = y1(j1:k1);
        y2_temp(j1:k1) = y2(j2:k2);
        x1c(i)=sum(y1_temp.*y2_temp);
    end

I compared the result with the Matlab xcorr function, and it seems the vector is reversed. Does this solve the error?

dieli
  • 179
  • 6