MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# can someone explain to me what is this entire code means?

Asked by lotus on 22 Mar 2013
`                                                                                                        ptotdB = dbp(ptot);`

in another file:

```                                                                                              function y=dbp(x)
y = -Inf*ones(size(x));
nonzero = x~=0;
y(nonzero) = 10*log10(abs(x(nonzero)));```

## Products

No products are associated with this question.

Answer by the cyclist on 22 Mar 2013
Edited by the cyclist on 22 Mar 2013

Looks like it's converting any non-zero input to decibels ( http://en.wikipedia.org/wiki/Decibel ), and zero input to negative infinity.

lotus on 24 Mar 2013

do i have to declare the input value for 'x'?

Walter Roberson on 24 Mar 2013

It depends what you mean by "declare". In order to use the function, you need to pass in an input value to the function, but the variable name you use in the calling function does not need to be named "x".

Answer by Ahmed A. Selman on 24 Mar 2013

This code finds log10 of the values of some vector (x). The line:

```y = -Inf*ones(size(x));
```

is meaningless in this part of the code because it is overridden later by:

```y(nonzero) = 10*log10(abs(x(nonzero)));
```

There could be more lines in this function that need the variable (nonzero). A much simpler code is:

```function y=dbp(x)
y = 10*log10(abs(x(:)))';
```

lotus on 24 Mar 2013

i tried it out.but still didn't get the desired answer.no changes at the answer.below is another subfile.i don't know which part that must be fixed to get the value of sfdr=70.9 and snr=60.7:

-mainfile- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Example 4.11 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Pipeline Converter % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all;

```                                                                                                %-------------------------------------------------------------------------%
%                         DAC Static parameters                           %
%-------------------------------------------------------------------------%```
```                                                                                                   %-------------------------------- Stage 1 --------------------------------%
V_DAC_L1=-0.5;
V_DAC_H1=0.5;
GAIN1=2.0;
VthL1=-0.25+eps;
VthH1=0.25+eps;```
```                                                                                             %-------------------------------- Stage 2 --------------------------------%
V_DAC_L2=-0.5;
V_DAC_H2=0.5;
GAIN2=2.0;
VthL2=-0.25+eps;
VthH2=0.25+eps;```
```                                                                                         %------------------------------- Stages 3-9 ------------------------------%
V_DAC_L3=-0.5;
V_DAC_H3=0.5;
GAIN3=2.0;
VthL3=-0.25+eps;
VthH3=0.25+eps;```
`                                                                                    Vth10=0;`
```                                                                                           %-------------------------------------------------------------------------%
%                             Dynamic parameters                          %
%-------------------------------------------------------------------------%```
```                                                                                           %-------------------------------- S&H Block-------------------------------%
srH=250e6;
f_TH=600e6;
betaH=1;
tauH=1/(2*pi*betaH*f_TH);
gainH=1;```
```                                                                                         %-------------------------------- Stage 1 --------------------------------%
sr1=10000e8;
f_T1=6000e6;
beta1=1/2;
tau1=1/(2*pi*beta1*f_T1);
gain1=1;```
```                                                                                           %-------------------------------- Stage 2 --------------------------------%
sr2=20000e8;
f_T2=6000e6;
beta2=1/2;
tau2=1/(2*pi*beta2*f_T2);
gain2=1;```
```                                                                                          %------------------------------- Stages 3-9 ------------------------------%
sr=25000e8;
f_T=6000e6;
beta=1/2;
tau=1/(2*pi*beta*f_T);
gain=1;```
```                                                                                             %-------------------------------------------------------------------------%
%                         Simulation parameters                           %
%-------------------------------------------------------------------------%```
```                                                                                         Ts=1e-8;
Tmax=Ts/2;
Vfs=2;
N=2^12;
Ntransient=20;
Tstop=Ts*(N+Ntransient);
nper=73;
Fs=1/Ts;
Fin=nper*Fs/N;                 % Input signal frequency (Fin = nper*Fs/N)
f=Fin/Fs;                      % Normalized signal frequency
bw=Fs/2;```
```                                                                                                   Amp_dB=-0;                     % Amplitude in dB
Ampl=10^(Amp_dB/20)*Vfs/2;     % Input signal amplitude```
`                                                                                 finrad=Fin*2*pi;`
```                                       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           Launch Simulation                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
options=simset('InitialStep', 1, 'RelTol', 1e-3, 'MaxStep', 1,...
'Fixedstep', 1);
```                                                                                             %-------------------------------------------------------------------------%
%                             Graphic Outputs                             %
%-------------------------------------------------------------------------%
w=hann(N);
f=Fin/Fs;                             % Normalized signal frequency
fB=N*(bw/Fs);                         % Base-band frequency bins
yfft=y(1+Ntransient:N+Ntransient);
[snr,ptot]=calcSNR(yfft',f,fB,w',N);
ptot=ptot-max(ptot);                  % Normalize total spectrum```
```                                                                                        figure(1);
clf;
plot(linspace(0,5E-9*Fs,N/2), ptot(1:N/2), 'r');
grid on;
title('PSD of the Output')
xlabel('Frequency [Hz]')
ylabel('PSD [dB]')
axis([0 5E-9*Fs -140 0]);
sfdr=max(ptot(nper+4:N/2))
snr```

-subfile calcSNR-

```                                                                                                                                                                      function [snrdB,ptotdB,psigdB,pnoisedB] = calcSNR(vout,f,fB,w,N,Vref)
% SNR calculation in the time domain (P. Malcovati, S. Brigati)
% vout: Sigma-Delta bit-stream taken at the modulator output
% f:    Normalized signal frequency (fs -> 1)
% fB:	Base-band frequency bins
% w:	windowing vector
% N:    samples number
% Vref: feedback reference voltage
%
% snrdB: 	 SNR in dB
% ptotdB: 	 Bit-stream power spectral density (vector)
% psigdB:	 Extracted signal power spectral density (vector)
% pnoisedB:  Noise power spectral density (vector)
%```
```                                                                                        Vref=1|-1
fB=ceil(fB);
signal=(N/sum(w))*sinusx(vout(1:N).*w,f,N);	% Extracts sinusoidal signal
noise=vout(1:N)-signal;			            % Extracts noise components
stot=((abs(fft((vout(1:N).*w)'))).^2);		% Bit-stream PSD
ssignal=(abs(fft((signal(1:N).*w)'))).^2;	% Signal PSD
snoise=(abs(fft((noise(1:N).*w)'))).^2;		% Noise PSD
pwsignal=sum(ssignal(1:fB));	            % Signal power
pwnoise=sum(snoise(1:fB));		            % Noise power
snr=pwsignal/pwnoise;
snrdB=dbp(snr);
norm=sum(stot)/Vref^2;						% PSD normalization
if nargout > 1
ptot=stot/norm;
ptotdB=dbp(ptot);
end```
```                                                                                                   if nargout > 2
psig=ssignal/norm;
psigdB=dbp(psig);
end```
```                                                                                                       if nargout > 3
pnoise=snoise/norm;
pnoisedB=dbp(pnoise);
end```

-subfile dbp-

```                                                                                                                                                                                                                       function y=dbp(x)
% dbp(x) = 10*log10(x): the dB equivalent of the power x
%y = -Inf*ones(size(x));
%nonzero = x~=0;
%y(nonzero) = 10*log10(abs(x(nonzero)));
y = 10*log10(abs(x(:)))';```

it is very pleasure if someone can help me with this kind of problems.thanking you in advance.

lotus on 24 Mar 2013

i got negative value for sfdr and snr=61.4315 which are not the desired answer.