Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to transform this short pyton code to matlab

Subject: how to transform this short pyton code to matlab

From: Armindo

Date: 11 May, 2014 11:17:06

Message: 1 of 2

Please someone can help me on this? I need to change this pyton code into matlab code. Any help would be appreciated. Please help I need to finish a project but for that I need this

if not len(fclim) or np.any(fclim < 0) or np.any(fclim > freq / 2):
        fc1 = 0
        fc2 = np.nonzero(freqs >= 0.9 * (freq / 2))[0][0]
        # log of exponential turns the problem to first order polynomial fit
        # make the data always greater than zero before taking the logarithm

       # Iam very confusing with this line of code
        reslog = np.log(np.abs(res[fc1:fc2 + 1] - res[fc2]) +
                        10 * np.finfo(np.float).eps)


        Blog, Alog = np.polyfit(freqs[fc1:fc2 + 1], reslog, 1)
        fcini = np.nonzero(freqs >= -3 / Blog) # 3 lifetimes
        fclim = [fcini[0][0], fc2] if np.size(fcini) else []

else:
        fclim = [np.nonzero(freqs >= fclim[0])[0][0],
                 np.nonzero(freqs >= fclim[1])[0][0]]

# I also need to know what is the equivalente of this in matlab

roots = UnivariateSpline(freqs, res - A, s=0).roots()

Subject: how to transform this short pyton code to matlab

From: dpb

Date: 11 May, 2014 14:40:31

Message: 2 of 2

On 5/11/2014 6:17 AM, Armindo wrote:
> Please someone can help me on this? I need to change this pyton code
> into matlab code. Any help would be appreciated. Please help I need to
> finish a project but for that I need this
>
> if not len(fclim) or np.any(fclim < 0) or np.any(fclim > freq / 2):
> fc1 = 0
> fc2 = np.nonzero(freqs >= 0.9 * (freq / 2))[0][0]
> # log of exponential turns the problem to first order polynomial fit
> # make the data always greater than zero before taking the logarithm
>
> # Iam very confusing with this line of code

> reslog =
> np.log(np.abs(res[fc1:fc2 + 1] - res[fc2]) +
> 10 * np.finfo(np.float).eps)

Looks to me like

   reslog=log(abs(res(fc1:fc2+1)-res(fc2)) + 10*eps);

but I "know nuthink" whatsoever of Python...

...

>
> # I also need to know what is the equivalente of this in matlab
>
> roots = UnivariateSpline(freqs, res - A, s=0).roots()

Overall, I'd think it simpler to just start w/ the requirement as given
by the comments and your intimate knowledge of the problem you're trying
to solve and write that in Matlab directly instead of literal
transcription by (apparently) rote.

I'd note it would seem that results wouldn't be very reliable in
application if it is true that data can be negative to use the abs()
operation and proceed this way. If it's just an unnecessary protection
against code error because the data can't be negative, that's something
else again.

There possibly are some Python-fluent folk here, but I'd think you'd get
better response if

a) you show your attempt first instead of ask for somebody to solve your
problem for you,

b) you were to describe the problem to be solved and asked for
suggestions on how, and especially

c) change the tone of urgency.

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us