Ho we run a section code one time in Matlab function block ? In below body I dont want assigning zeros in Dout after 1st iteration. This matlab function block run 100 times and problem is it every time set zeros in dout.

6 次查看(过去 30 天)
function y = ADC(u, clk, count)
%function y = fcn(u, clk)
count=count+1;
% if clk==1
% disp('hello');
% end
% emulation of SAR ADC operation
Nbit = 7;
Vref = 64;
VCM = Vref/2;
%dVin = (-37298:37299)/37299*Vref;
Point=u;
dVin = (-Point:Point)/Point*Vref;
%plot(dVin);
Vip = dVin/2+VCM;
Vin = -dVin/2+VCM;
N = length(dVin);
k=count;
*Dout = zeros(1,N);
B = zeros(1,Nbit);*
while k <=N
if clk==1
Vxp = Vip(k);
Vxn = Vin(k);
for kbit = 1:Nbit
if Vxp - Vxn > 0
B(kbit) = 1;
Vxp = Vxp - Vref*2^(-kbit);
else
B(kbit) = 0;
Vxn = Vxn - Vref*2^(-kbit);
end
end
Dout(k) = B(1)*64 + B(2)*32 + B(3)*16 + B(4)*8 + B(5)*4 + B(6)*2 + B(7)*1 -64 +0.5;
% k=k+1;
break;
else
%k=k+1;
break
end
end
plot(dVin,Dout,'r*')
y = Dout;

采纳的回答

Honglei Chen
Honglei Chen 2018-10-2
You can make Dout persistent so it preserves the value between runs, like this
persistent Dout;
if isempty(Dout)
Dout = zeros(1,N);
end
HTH
  14 个评论
Walter Roberson
Walter Roberson 2018-10-5
You should be using a triggered subsystem if the code is not to run at all when the control signal is 0.
Note: in such a case you would no longer need clk1 as an input, as the reconfigured block would only get run when clk1 was 1.

请先登录,再进行评论。

更多回答(1 个)

Honglei Chen
Honglei Chen 2018-10-5
Do you get an error? What is the error message? Looks like you may have some dimension issues? An alternative way is to control the calling of this block, like in a subsystem so it does not get triggered when clock is 0.
HTH
  3 个评论
Sarfaraz Ahmed
Sarfaraz Ahmed 2018-10-6
编辑:Sarfaraz Ahmed 2018-10-6
Thanks. I am using this with delay but no effect it sample at same time. how can I do this so that it sample according to clock delay. I think the clock is not properly time based . I attached a snap please have a look and my sampler code below:
function Vin = sample(ip) coder.extrinsic('stem');
persistent x1;
if isempty(x1) % Initialization x1=0; end
persistent M;
if isempty(M) % Initialization M=1; end
% Sampling input signal1
f=10e3; %Frequency of sinusoid nCyl=5; %generate five cycles of sinusoid %(0:2^-6:5^-4) fs1=50e3; %50kHz sampling rate t1=0:1/fs1:nCyl*1/f; %time index size= length(t1);
%if clk1==1
while M<=size
x1=sin(2*pi*f*t1(M));
stem(t1(M),x1); % plotting discrete samples on the input signal1
M=M+1;
break;
end
%end
Vin=x1;
what if I use "pulstran" function and embed in subblock ? but when i use this function it shows an error "Function output 'y' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type." The code for pulse is :
function y = pulse() coder.extrinsic('pulstran');
t=0:0.01:10; %Time vector w = 1; %pulse width d= w/2:w*2:10; %delay vector y2=pulstran(t,d,'rectpuls',w); subplot(2,1,1); plot(t,y2); set(gca,'Ylim',[-0.1 1.1]); xlabel('Time(s)');
y=y2;
I want it sample whenever clock pusle at the rising edge so when it with delays it should sample with delay. Please help me in this regard. Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by