How to find Shift phase between two sinusoids in Python.
For example, I created two sinusoid with phase shift 180 radian (Visually). Can we calculate the phase shift in python script if we know only graph_1 and graph_2?
import matplotlib.pyplot as plt
import numpy as np
data=[]
def sin (f):
x=np.array(range(1,200))
y = 10*np.sin((0.1*x)+f)
return (y)
import matplotlib.pyplot as plt
graph_1 = sin(3.12)
graph_2 = sin(0)
plt.plot(graph_1 ,graph_2)
plt.show()
Please see the image here
Related
I am trying to calculate the power spectral density step by step using numpy FFT and comparing it to the PSD calculated by pylab PSD. Difference b/w plots of both methods can be visualized from in figures below.
#*************************PSD using pylab PSD**************************
import math
from rtlsdr import RtlSdr
import pylab as mpl
import numpy as np
import pandas as pd
sdr = RtlSdr()
sdr.rs = 2.4e6
sdr.fc = 935e6
sdr.gain = 40
samplest = ()
psd_scant = ()
psd_logt = ()
samples = sdr.read_samples(256*1024)
psd_scan, f=mpl.psd(samples, NFFT=1024, Fc=sdr.fc/1e6, Fs=sdr.rs/1e6)
psd_log = 10*np.log10(psd_scan)```
#*************************Step by Step PSD using FFT**************************
n=256
x= np.array_split(samples, n)
xdf = pd.DataFrame(x)
xdft =xdf.transpose()
NFFT = 1024
xfft = np.fft.fft(xdft, n=1024, axis=0)[:1024, :]
xfftt=xfft.transpose()
PSD = np.abs(xfftt)**2
PSD_log = 10*np.log10(PSD)
PSD_shifted = np.fft.fftshift(PSD_log)
Final = PSD_shifted.mean(axis=0)
plt.xlabel('Frequency'); plt.ylabel('PSD')
plt.grid('on')
plt.plot(f,Final)
Pylab PSD
PSD using FFT
**Hints/ Help required **
To compute the manual PSD plot same as pylab PSD will be appreciated.
How the y axis can be corrected.
I'm trying to create a graph with k_b as the x-value and delta_P as the y-value. I want to plot k_b against delta_P but S=3 for one curve and S=0.1 for another curve. However, I want the two lines to be on the same graph. Does anyone have any advice on how to do that? Below is what I have for S=3 and it works.
def rocproduct(k_cat,E0,S,k_b,k_f):
return k_cat*E0*S/((k_b/k_f)+S)
import numpy as np
import matplotlib.pyplot as plt
k_cat=0.1;E0=1;k_f=0.3;S=3
k_b=np.array([0.01,0.1,0.2,0.5,1,1.5,2,5,10])
delta_P=rocproduct(k_cat,E0,S,k_b,k_f)
plt.ylabel('rate of change of product')
plt.xlabel('kb')
plt.plot(k_b,delta_P)
Just call rocproduct for S=0.1 and plot it again
import numpy as np
import matplotlib.pyplot as plt
# Parameters
k_cat=0.1
E0=1
k_f=0.3
S=3
# Function for data
def rocproduct(k_cat,E0,S,k_b,k_f):
return k_cat*E0*S/((k_b/k_f)+S)
# Data to plot
k_b=np.array([0.01,0.1,0.2,0.5,1,1.5,2,5,10])
delta_P_1=rocproduct(k_cat,E0,S,k_b,k_f)
S = 0.1
delta_P_2=rocproduct(k_cat,E0,S,k_b,k_f)
# Plotting
plt.ylabel('rate of change of product')
plt.xlabel('kb')
plt.plot(k_b,delta_P_1)
plt.plot(k_b, delta_P_2)
plt.show()
I have created a list of values of Shannon entropy for a pair of multiple sequence aligned sequences. While plotting the values I get a simple plot. I want to plot a smooth curve over the lines. Can anyone suggest to me what will be the right way to process it? BAsically I want to plot a smooth curve that touches the tip of every bar and goes to zero where the "y axis value" is zero.
link for image: [1]: https://i.stack.imgur.com/SY3jH.png
#importing the relevant packages
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.interpolate import make_interp_spline
from Bio import AlignIO
import warnings
warnings.filterwarnings("ignore")
#function to calculate the Shannon Entropy of a MSA
# H = -sum[p(x).log2(px)]
def shannon_entropy(list_input):
unique_aa = set(list_input)
M = len(list_input)
entropy_list = []
# Number of residues in column
for aa in unique_aa:
n_i = list_input.count(aa)
P_i = n_i/float(M)
entropy_i = P_i*(math.log(P_i,2))
entropy_list.append(entropy_i)
sh_entropy = -(sum(entropy_list))
#print(sh_entropy)
return sh_entropy
#importing the MSA file
#importing the clustal file
align_clustal1 =AlignIO.read("/home/clustal.aln", "clustal")
def shannon_entropy_list_msa(alignment_file):
shannon_entropy_list = []
for col_no in range(len(list(alignment_file[0]))):
list_input = list(alignment_file[:, col_no])
shannon_entropy_list.append(shannon_entropy(list_input))
return shannon_entropy_list
clustal_omega1 = shannon_entropy_list_msa(align_clustal1)
# Plotting the data
plt.figure(figsize=(18,10))
plt.plot(clustal_omega1, 'r')
plt.xlabel('Residue', fontsize=16)
plt.ylabel("Shannon's entropy", fontsize=16)
plt.show()
Edit 1:
Here is what my graph looks like after implementing the "pchip" method. link for the pchip output: https://i.stack.imgur.com/hA3KW.png
pchip monotonic spline output
One approach would be to use PCHIP interpolation, which will give you the monotonic curve with the required behaviour for zero values on the y-axis.
We can't run your exact code example on our machines because you point to a local Clustal file in your 'home' directory.
Here's a simple working example, with link to output image:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import pchip
mylist = [10,0,0,0,0,9,9,0,0,0,11,11,11,0,0]
mylist_np = np.array(mylist)
samples = np.array(range(len(mylist)))
xnew = np.linspace(samples.min(), samples.max(), 100)
plt.plot(xnew,pchip(samples, mylist_np )(xnew))
plt.show()
I have a vector for velocity of a time series. for example :
u=[100,120,150,115,130,115,105,103,108,132,135,121]
now I need to calculate Δu and then draw a scatter plot.
something like the picture below.
how can I do that?
import numpy as np
import matplotlib.pyplot as plt
u = np.array([100,120,150,115,130,115,105,103,108,132,135,121])
du = u[1:] - u[:-1] # the difference between the current and the prior velocity
plt.scatter(u[1:],du)
plt.show()
Assuming Δu = u[i] - u[i-1], It is obvious you should use this formula for u[1:].
So just simply use these lines of codes:
from numpy import array
import matplotlib.pyplot as plt
u = aray(u)
del_u = u[1:] - u[:-1] # This line let you use that formula for all numbers in array except first one.
plt.scatter(u[1:], del_u)
plt.show()
I am using the below python code so as to bias an absolute sine wave. I would like to have only the crest part of the wave and not the trough part even after positive biasing.Here I am unable achieve continuous crest signal after positive biasing. Can any one help me in this?
Usage: Keeping the input signals above the threshold even during dynamic shift of threshold.
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x=np.linspace(-20,20,1000);
y=np.abs(np.sin(x)+Bias);
#Bias=np.zeros_like(x); # This is not working
y[(y<=Bias)]= Bias + y # This is not working
plt.plot(x,y)
plt.grid()
plt.show()
It is a litle bit unclear what you are asking... Maybe you want to try this:
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x = np.linspace(-20, 20, 1000);
y = np.abs(np.sin(x))
y = y + Bias
plt.plot(x, y)
plt.grid()
plt.show()
or this:
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x=np.linspace(-20,20,1000);
y=np.abs(np.sin(x) + Bias);
y[(y<=Bias)]= Bias
plt.plot(x,y)
plt.grid()
plt.show()