Convert matlab to VHDL
이전 댓글 표시
This is my Matlab code and i am trying to change it to VHDL:
%linear dimension of membrane -- bigger is lower pitch
nx = 5 ; ny=50;
u = zeros(nx,ny); %time t
u1 = zeros(nx,ny); %time t-1
u2 = zeros(nx,ny); %time t-2
uHit = zeros(nx,ny); %input strike
% 0 < rho < 0.5 -- lower rho => lower pitch
% rho = (vel*dt/dx)^2
rho = 0.5;
% eta = damping*dt/2
% higher damping => shorter sound
eta = 0.0002 ;
% boundary condition -1.0<gain<1.0
% 1.0 is completely free edge
% 0.0 is clamped edge
boundaryGain = 0.0;
%time
Fs = 8000 ;
time = 0.0:1/Fs:5.0;
%output sound
aud = zeros(size(time)) ;
%sets the amplitude of the stick strike
ampIn = .5;
%sets the position of the stick strike: 0<pos<n
x_mid = 2;% nx/2;
y_mid = 2; %ny/2;
%sets width of the gaussian strike input -- see figure 1
alpha = .5 ;
%compute the gaussian strike amplitude
for i=2:nx-1
for j=2:ny-1
uHit(i,j) = ampIn*exp(-alpha*(((i-1)-x_mid)^2+((j-1)-y_mid)^2));
end
end
%enforce boundary conditions
uHit(1,:) = boundaryGain * uHit(2,:);
uHit(nx,:) = boundaryGain * uHit(nx-1,:);
uHit(:,1) = boundaryGain * uHit(:,2);
uHit(:,ny) = boundaryGain * uHit(:,ny-1);
figure(1);clf;
mh = mesh(uHit);
title('initial displacement')
set(gca, 'zlim', [-0.5,0.5])
%index for storing audio output
tindex = 1;
for t=time
%animate drum -- very SLOW
% set(mh, 'zdata', u)
% drawnow
% use the central sample for output
% any other sample or sum is also valid to tr
aud(tindex) = u(2,2) ; %sum(sum(u))/(n^2); %
%vectorized update of positions, except for boundaries
u(2:nx-1,2:ny-1) = 1/(1+eta) * (...
rho * (u1(3:nx,2:ny-1)+u1(1:nx-2,2:ny-1)+u1(2:nx-1,3:ny)+u1(2:nx-1,1:ny-2)-4*u1(2:nx-1,2:ny-1)) ...
+ 2 * u1(2:nx-1,2:ny-1) ...
- (1-eta) * (u2(2:nx-1,2:ny-1)) ...
) ;
%enforce boundary conditions
u(1,:) = boundaryGain * u(2,:);
u(nx,:) = boundaryGain * u(nx-1,:);
u(:,1) = boundaryGain * u(:,2);
u(:,ny) = boundaryGain * u(:,ny-1);
%enforce boundary conditions
% u(1,:) = boundaryGain * (u(2,:)-u1(2,:));
% u(n,:) = boundaryGain * (u(n-1,:)-u1(n-1,:));
% u(:,1) = boundaryGain * (u(:,2)-u1(:,2)) ;
% u(:,n) = boundaryGain * (u(:,n-1)-u1(:,n-1));
%update history
u2 = u1;
u1 = u;
%apply drum strikes at two times
if t==0 | t==1.0
u1 = u1 + uHit;
end
tindex = tindex + 1;
end
% get the spectrum
figure(2); clf;
Hs=spectrum.welch('Hamming',1024,50);
psd(Hs,aud,'Fs',Fs)
title('Free edge plate spectrum')
set(gca,'xlim',[0 2])
%play the sound
sound(aud, Fs)
wavwrite(aud, Fs, 16, 'bell.wav')
%end
답변 (3개)
sai sudha
2013년 2월 20일
1 개 추천
HOW TO CONVERT MATLAB CODE TO VHDL CODE..,CAN ANYONE HELP ME IN THIS ASPECT?
Walter Roberson
2011년 3월 31일
0 개 추천
Okay, so what problems are you encountering when you try? Are you using the Simulink HDL Coder toolbox?
댓글 수: 2
Amad
2011년 3월 31일
Hafsa Iqbal
2017년 12월 4일
hey... I have to convert MATLAB code to VHDL. Is there any way to directly convert it into VHDL?
Tim McBrayer
2011년 4월 11일
You will need to use Simulink HDL Coder in order to automatically convert MATLAB code to VHDL or Verilog. You can run:
ver('hdlcoder')
to see if you have Simulink HDL Coder installed and licensed. If you do have the tool available, you will want to consider the MATLAB Function Block for generating code from your MATLAB code. The demo "An 8-bit RISC Processor using MATLAB Function Blocks" is a good place to start understanding the use of the MATLAB Function block for HDL code generation.
댓글 수: 2
Geethu
2012년 1월 5일
I want to implement my matlab code in FPGA. Can you advise which one to use system generator or simulink hdl coder? Matlab coding part is over and I don't know simulink and needs to complete my project by February end. Can anyone help?
Diogo
2013년 2월 19일
I have the same issue, did you managed to get your project done?
카테고리
도움말 센터 및 File Exchange에서 Code Generation and Deployment에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!