Conversion of MATLAB (.m) coding into Verilog HDL
36 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
hamid
am 16 Sep. 2011
Beantwortet: Ghouse Ahamed Z
am 4 Jul. 2025
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 16 Sep. 2011
Bearbeitet: MathWorks Support Team
am 8 Jan. 2024
***** 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 Kommentar
Fangjun Jiang
am 16 Sep. 2011
Embedded MATLAB belongs to Simulink. With MATLAB alone, no need and can't use the Embedded MATLAB Function block.
Weitere Antworten (4)
Fangjun Jiang
am 16 Sep. 2011
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.
0 Kommentare
hamid
am 16 Sep. 2011
1 Kommentar
Walter Roberson
am 16 Sep. 2011
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
am 23 Mär. 2023
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 Kommentare
Kiran Kintali
am 23 Mär. 2023
Bearbeitet: Kiran Kintali
am 23 Mär. 2023
>> 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
am 25 Aug. 2023
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
am 4 Jul. 2025
//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
0 Kommentare
Siehe auch
Kategorien
Mehr zu HDL Coder finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!