Conversion of MATLAB (.m) coding into Verilog HDL

37 ビュー (過去 30 日間)
hamid
hamid 2011 年 9 月 16 日
回答済み: Ghouse Ahamed Z 2025 年 7 月 4 日
Dear All
I am doing a project in which i have to convert image encryption MATLAB code into Verilog HDL language code and then implemented it on DE2 Board. I have already searcehd a lot of questions regarding this matter but every one said that you have to write MATLAB code either into Embedded MATLAB code or on Simulink. My question is that is this possible that i write a code in simple MATLAB coding means in .m extension (not on Simulink or in Embedded MATLAB code) and convert it into Verilog HDL Langugae. Because i have only .m extension MATLAB code to convert.
Looking forward for a positive response and thanking all in advance.
Thanks Regards

採用された回答

Walter Roberson
Walter Roberson 2011 年 9 月 16 日
編集済み: MathWorks Support Team 2024 年 1 月 8 日
***** Update by MathWorks Technical Support team*****
>> mlhdlc_demo_setup('aes')
You have to structure the code into design and testbench. See the above example for insights. Follow the workflow steps shown here...
This page covers several tutorials showing how to generate HDL from MATLAB code.
****************************************************************
You would probably have had to restrict the code to using the functions supported by Embedded MATLAB (even if not written with specific reference to Embedded MATLAB), and then you would probably have to run the code through MATLAB Coder (for Release R2011a or later). The result would be a C file, which you could then compile to HDL such as with one of these tools.
MATLAB Coder does not rely on any other toolboxes, but it seems likely to me that it is fairly expensive. I do not know, though, how the price would compare the alternatives.
  1 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 16 日
Embedded MATLAB belongs to Simulink. With MATLAB alone, no need and can't use the Embedded MATLAB Function block.

サインインしてコメントする。

その他の回答 (4 件)

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 16 日
What you heard is probably right. If you are looking for a Yes answer, you are probably going to be disappointed. Look at all the products at http://www.mathworks.com/products/?s_cid=global_nav, there are only two mentions of HDL. One is "Filter Design HDL Coder" toolbox under MATLAB. The other is "Simulink HDL Coder" toolbox under Simulink. That means you need to have at least one of the toolbox to be able to generate a HDL code. With only MATLAB, it can't be done at this point.

hamid
hamid 2011 年 9 月 16 日
Dear Walter!!! I have Matlab Release 2011a so can you Please explain how i can do conversion? also you wrote "fairly Expensive", i didn't get u clearly..Please also explain this. If you are talking about different toolboxes to be used in MATLAB then please tell me, i will try to arrange them.
Dear Fangjun!!! beside these two method is there any else method or i have to write the whole code by myself in Simulink or Embedded Matlab?
Thanks
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 9 月 16 日
I do not have a price chart available, and I did not pay much attention to the MATLAB Coder price the last time I glanced at the chart. I think it was over $7000 for MATLAB Coder, but you would need to check with Sales to be sure (especially if you can get academic pricing.)
I did not investigate the C to HDL tools to see if any of them were free (and worth owning). It appears that many of them are specialized commercial products that I would expect would cost thousands of dollars.

サインインしてコメントする。


Hemanth Kumar
Hemanth Kumar 2023 年 3 月 23 日
clearvars
close all
clc
nPUs = 1; % Number of primary users
nSUs = 2; % Number of secondary users
nSamps = 1e3; % Number of sensing samples per sensing period
snrdB = linspace(-12,-8,nSUs); snr = db2pow(snrdB); % Signal-to-noise ratio (SNR) of each SU
sPow = ones(1,nSUs); % Received PU signal power at each SU
nPow = sPow./snr; % Noise power at each SU
nMCEvents = 5e3; % Number of Monte Carlo events
nROCPts = 50; % Number of receiver operating characteristic (ROC) curve points
Ted = zeros(nMCEvents,1); % Preallocating for speed
TX = randi([0,1],nMCEvents,1); % Simulates hypotheses H0 and H1 for the received signal, 50% under H0 and 50% under H1
for i = 1:nMCEvents
H = sqrt(1/2)*complex(randn(nSUs,nPUs),randn(nSUs,nPUs)); % Rayleigh channel samples for each SU
X = sqrt(1/nPUs)*complex(randn(nPUs,nSamps),randn(nPUs,nSamps)); % Normally distributed primary user signal (weighted by sqrt(1/nPUs) for keeping desired SNR)
V = sqrt(diag(nPow)/2)*complex(randn(nSUs,nSamps),randn(nSUs,nSamps)); % AWGN
Y = TX(i)*sqrt(diag(sPow)/2)*(H*X) + V; % Received signal at each SU
Ted(i) = (1/nSUs)*sum((1/nSamps)*sum(abs(Y).^2,2)); % Energy detection test statistic
end
Thres = linspace(min(Ted),0.85*max(Ted),nROCPts); % Threshold decision for each ROC point
nH0 = sum(TX==0); % The number of times the PU transmitter(s) has been in the non-active state
nH1 = sum(TX==1); % The number of times the PU transmitter(s) has been in the active state
Pfa = sum(Ted>Thres&TX==0)/nH0; % Probability of false alarm for each decision threshold
Pd = sum(Ted>Thres&TX==1)/nH1; % Probability of detection for each decision threshold
figure
plot(Pfa,Pd)
title('ROC curve')
xlabel('Probability of false alrm')
ylabel('Probability of detection')
legend('ED','Location','SouthEast')
grid
  3 件のコメント
Kiran Kintali
Kiran Kintali 2023 年 3 月 23 日
編集済み: Kiran Kintali 2023 年 3 月 23 日
>> mlhdlc_demo_setup('aes')
You have to structure the code into design and testbench. See the above example for insights. Follow the workflow steps shown here...
This page covers several tutorials showing how to generate HDL from MATLAB code.
https://www.mathworks.com/matlabcentral/fileexchange/50098-matlab-hdlcoder-examples
Rehannara Beegum Thajudeen
Rehannara Beegum Thajudeen 2023 年 8 月 25 日
I have a doubt in the line, Thres = linspace(min(Ted),0.85*max(Ted),nROCPts);.....here what is the significance of multiplying 0.85 with Ted? How we decide this constant value?

サインインしてコメントする。


Ghouse Ahamed Z
Ghouse Ahamed Z 2025 年 7 月 4 日
//Verilog code
module energy_detector #(
parameter N = 1024, // Number of samples
parameter DATA_WIDTH = 16 // Bit width of input
)(
input clk,
input rst,
input start,
input signed [DATA_WIDTH-1:0] signal_in,
input valid_in,
output reg done,
output reg [2*DATA_WIDTH+10:0] energy_out // Sufficient width for accumulation
);
reg [15:0] count;
reg [2*DATA_WIDTH+10:0] acc;
always @(posedge clk or posedge rst) begin
if (rst) begin
count <= 0;
acc <= 0;
energy_out <= 0;
done <= 0;
end else if (start) begin
if (valid_in) begin
acc <= acc + signal_in * signal_in;
count <= count + 1;
if (count == N - 1) begin
energy_out <= acc + signal_in * signal_in; // last sample
done <= 1;
count <= 0;
acc <= 0;
end
end
end
end
endmodule
// testbench
`timescale 1ns/1ps
module tb_energy_detector;
parameter N = 1024;
parameter DATA_WIDTH = 16;
reg clk, rst, start, valid_in;
reg signed [DATA_WIDTH-1:0] signal_in;
wire done;
wire [2*DATA_WIDTH+10:0] energy_out;
energy_detector #(N, DATA_WIDTH) uut (
.clk(clk),
.rst(rst),
.start(start),
.signal_in(signal_in),
.valid_in(valid_in),
.done(done),
.energy_out(energy_out)
);
// Clock generation
always #5 clk = ~clk;
integer i;
reg signed [DATA_WIDTH-1:0] signal_mem [0:N-1];
initial begin
// Initialize signals
clk = 0;
rst = 1;
start = 0;
valid_in = 0;
signal_in = 0;
// Sample signal: energy of noisy signal (simulate PU presence or absence)
for (i = 0; i < N; i = i + 1) begin
signal_mem[i] = $random % 100; // Replace with meaningful PU signal
end
#20 rst = 0;
#10 start = 1;
for (i = 0; i < N; i = i + 1) begin
valid_in = 1;
signal_in = signal_mem[i];
#10;
end
valid_in = 0;
start = 0;
#100;
$display("Energy Output = %d", energy_out);
$finish;
end
endmodule

カテゴリ

Help Center および File ExchangeHDL Coder についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by