Plotting discrete piecewise function - signal - python-3.x

I need to plot a discrete signal that is defined by a piecewise function :
if n < 0 , x = (135/56) * (0.9)^n - (1/8)(0.1)^n - (2/7)(0.2)^n
if 0<=n< 4, x = (135/56) * (0.9)^n + (7/8)(0.1)^n + (5/7)(0.2)^n
if n>=4 , x = (135/56) * (0.9)^n + (7/8)(0.1)^n + (5/7)(0.2)^n + (0.1)^(-4) + (0.2)^(-4)
I have searched a lot in web and especially here and I came up with this code , that after many corrections it actually runned in spyder. But the result is definetely not the expected one. Can anyone help me?
import numpy as np
import matplotlib.pyplot as plt
xPoints = []
nPoints = []
q = (135 / 56)
z= -(1/8)
r = -(2/7)
m = 7/8
p = 5 /7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
for n in range(-5,11):
if n<0 :
x = q *np.power(0.9, n) + z* np.power(0.1, n) + r* np.power(0.2, n)
elif (n>=0 and n<4):
x =q *np.power(0.9, n) + m* np.power(0.1, n) + p* np.power(0.2, n)
else:
x =q *np.power(0.9, n) + m* np.power(0.1, n) + p* np.power(0.2, n)+ j + a
xPoints.append(x)
nPoints.append(n)
plt.plot(nPoints, xPoints)
plt.plot.show()

In numpy stepwise functions can be created using where. One of numpy's most magical features is broadcasting, where a function can be called on a complete array of values at once.
Your example code creates the expected curve, but only adds a point at integer values. To create a smooth curve, np.linspace creates a long array of values (the code below uses 1000 little steps between -5 and 5). (Note that numpy needs the & operator for a logical and of two array expressions. In this particular case you could use n < 4 instead of (n >= 0) & (n < 4) as the case of n < 0 is taken care of earlier.)
import numpy as np
import matplotlib.pyplot as plt
q = (135 / 56)
z = -(1 / 8)
r = -(2 / 7)
m = 7 / 8
p = 5 / 7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
n = np.linspace(-5, 5, 1000)
x = np.where(n < 0, q * np.power(0.9, n) + z * np.power(0.1, n) + r * np.power(0.2, n),
np.where((n >= 0) & (n < 4), q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n),
q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n) + j + a))
plt.plot(n, x)
plt.show()
If you only want the integer positions, you can use np.arange instead of np.linspace and then create a scatter plot (plt.scatter(n, x)) or maybe a stemplot:
import numpy as np
import matplotlib.pyplot as plt
q = (135 / 56)
z = -(1 / 8)
r = -(2 / 7)
m = 7 / 8
p = 5 / 7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
n = np.arange(-5, 6)
x = np.where(n < 0, q * np.power(0.9, n) + z * np.power(0.1, n) + r * np.power(0.2, n),
np.where((n >= 0) & (n < 4), q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n),
q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n) + j + a))
plt.stem(n, x)
plt.show()

Related

Looking for simplification of Elliptic curve multiplication calculator

I am looking for a reduced way of this code. I had to do the division separately because of the multiplicative inverse condition.
"""This code calculates the multiplication Elliptic curves over Zp"""
#Initial values for testing
yq = 3
yp = 3
xq = 8
xp = 8
a = 1
p = 11
#Calculate the Euclid greatest common divisor
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return g, x - (b // a) * y, y
#Calculate the multiplicate inverse
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('Mod inverse does not exist')
else:
return x % m
#veces = Number of times for the multiplication
veces = 7
print(f"The next results are the multiplication of {veces}*({xp},{yp})")
z = 1
while z <= (veces - 1):
if xp == xq and yp == yq:
numerador = (3 * pow(xp, 2) + a) % p
denominador = ((2 * yp) % p)
inver = modinv(denominador, p)
landa = (inver * numerador) % p
else:
numerador = (yq - yp) % p
denominador = (xq - xp) % p
inver = modinv(denominador, p)
landa = (inver * numerador) % p
xr = (pow(landa, 2) - xp - xq) % p
yr = (landa * (xp - xr) - yp) % p
z += 1
xp, yp = xr, yr
print(f"The result is ({xp},{yp})")
#Any way to simplify this code? I had to do the division separately but I think we can a reducted code for the division.

How to properly convert hsl colors to rgb colors in lua?

I have the following code:
#!/usr/bin/env lua5.3
-- Code adapted from https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua#L51
local function hslToRgb(h, s, l, a)
local r, g, b
h = (h / 255)
s = (s / 100)
l = (l / 100)
if s == 0 then
r, g, b = l, l, l -- achromatic
else
local function hue2rgb(p, q, t)
if t < 0 then t = t + 1 end
if t > 1 then t = t - 1 end
if t < 1/6 then return p + (q - p) * 6 * t end
if t < 1/2 then return q end
if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
return p
end
local q
if l < 0.5 then q = l * (1 + s) else q = l + s - l * s end
local p = 2 * l - q
r = hue2rgb(p, q, h + 1/3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h - 1/3)
end
if not a then a = 1 end
return r * 255, g * 255, b * 255, a * 255
end
local h,s,l,a
h,s,l,a = hslToRgb(220, 16.4, 21.6)
print(h,s,l,a)
-- expected output: 46 52 64 255
-- actual output: 64.11312 46.04688 60.92496 255
But, as stated at the end, the color values it outputs are completely wrong. The decimals are not an issue (as in, it's not an issue that it outputs them; their values are still wrong).
A hue value it's calculated in degrees, so the max isn't 255, but 360:
function hslToRgb(h, s, l)
h = h / 360
s = s / 100
l = l / 100
local r, g, b;
if s == 0 then
r, g, b = l, l, l; -- achromatic
else
local function hue2rgb(p, q, t)
if t < 0 then t = t + 1 end
if t > 1 then t = t - 1 end
if t < 1 / 6 then return p + (q - p) * 6 * t end
if t < 1 / 2 then return q end
if t < 2 / 3 then return p + (q - p) * (2 / 3 - t) * 6 end
return p;
end
local q = l < 0.5 and l * (1 + s) or l + s - l * s;
local p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
end
if not a then a = 1 end
return r * 255, g * 255, b * 255, a * 255
end
You can see this code working here.
I have been working on a more elegant solution to the HSV to RGB problem for a little bit now and this is what I've come up with.
local ceil = math.ceil
local abs = math.abs
local function clamp(v, min, max)
if v < min then return min end
if v > max then return max end
return v
end
local function HSV(h, s, v)
local vert = ceil(h / 120)
local r = abs(((h / 60) - 2 * vert))
local r, g, b = clamp(r, 1 - s, 1), clamp(2 - r, 1 - s, 1), (1 - s * v)
if vert == 1 then return r, g, b end
if vert == 2 then return b, r, g end
if vert == 3 then return g, b, r end
end
-- HSV to RGB
min = math.min
max = math.max
abs = math.abs
local function HSV2RGB (h, s, v)
local k1 = v*(1-s)
local k2 = v - k1
local r = min (max (3*abs (((h )/180)%2-1)-1, 0), 1)
local g = min (max (3*abs (((h -120)/180)%2-1)-1, 0), 1)
local b = min (max (3*abs (((h +120)/180)%2-1)-1, 0), 1)
return k1 + k2 * r, k1 + k2 * g, k1 + k2 * b
end

Heron's method in haskell

I'm getting divide by zero exceptions in this code of heron's method, and I am kind of lost here.
epsilon:: Integral a => a
epsilon = 1
heron:: Integral a => a -> a
heron r = help 0
where
help x
| abs (heron' x - heron' (x + 1)) < epsilon = heron' (x + 1)
| otherwise = help (x + 1)
heron' 0 = 1
heron' x = (1 `div` 2) * (heron' (x-1) + (r `div` heron' (x-1)))
Any suggestions where in this code I have to look to solve this problem?
(1 `div` 2) is definitely a problem , but what do I need to write instead?
If you need division of this kind, you probably want to use (/) instead of div and Fractional instead of Integral. So:
epsilon:: Fractional a => a
epsilon = 1
heron:: (Fractional a, Ord a) => a -> a
heron r = help 0
where
help x
| abs (heron' x - heron' (x + 1)) < epsilon = heron' (x + 1)
| otherwise = help (x + 1)
heron' 0 = 1
heron' x = (1 / 2) * (heron' (x-1) + (r / heron' (x-1)))

I'm trying to calculate pi in python3 using infinite loops:

answer=0
for i in range (11):
k=i
x = (-1**k) / ((2 * k) + 1)
answer+=x
answer=4*answer
print(answer)
output: -8.723498311114408
it should be close to 3.14
** has precedence over - (see the docs), hence -1 ** k evalutes to -(1 ** k) which is of course not what you meant.
Change x = (-1 ** k) / ((2 * k) + 1) to x = (-1) ** k / ((2 * k) + 1)

Why do I get the "Type-declaration character does not match declared data type" in my VBA code?

I wrote a program which calculates the integral of the probability density function of the lognormal distribution.
I want it to be able to calculate the nth moments of the pdf too. But if I modify my code I get this error. It works properly before I add *(x^(M))
Function riemanint(n, a, b)
Dim h, i, x
h = (b - a) / n
x = a - h / 2
For i = 1 To n
x = x + h
s = s + f(x, ev, var, PI, M) * h
Next
riemanint = s
End Function
Function f(x, ev, var, PI, M)
PI = Application.WorksheetFunction.PI()
ev = Range("D2")
var = Range("E2")
M = Range("F2")
f = (((1 / (x * ((2 * PI) ^ 0.5) * var)) * Exp((-(Log(x) - ev) ^ 2) / (2 * var * var)))) * (x^(M))
End Function

Resources