I was trying to simulate a square wave with a duty cycle but something went wrong and i've been spending the last 3 hours understandig why.
Here are the pictures of what the function i have to use (1), what i should get (2), what I get (3) and the script (4).
https://imgur.com/a/mcbwoyS
δ varies from o to 1 (in my plot it was 0.6)
Here is my code
#libraries
import numpy as np
from matplotlib import pyplot as plt
#function
R=3290
C=1.1693*10**(-6)
f=1000
ft=10
tau=(ft*2*np.pi)**-1
delta=tau*f
D=0.6
x=np.linspace(-2/f,2/f, 4999)
k=np.zeros(5000)
i=0
k[0]=1
while i<5000:
k[i]=2*i+1
i+=1
ck = (2/(np.pi*k))*np.sin(k*np.pi*D)
omegak=(2*np.pi)*k*f
Ak=1/(np.sqrt(1+(k*f/ft)**2))
Dphik=np.arctan(-k*f/ft)
g=np.zeros(4999)
for j in range(0,4999):
g[j]=np.sum(ck*np.cos(omegak*x[j]))
#plot
plt.figure(figsize=(10,5))
plt.plot(x, g)
plt.xlabel('t[T]')
plt.ylabel('g(t)')
plt.show()
As you can see from the pictures i doesn't really work that well and i dont know why.
Related
I'm having trouble trying to display my results after the program is finished. I'm expecting to see a velocity vs position graph, but for some reason, it's not showing up. Is there something wrong with my code.
import numpy
from matplotlib import pyplot
import time,sys
#specifying parameters
nx=41 # number of space steps
nt=25 #number of time steps
dt=0.025 #width between time intervals
dx=2/(nx-1) #width between space intervals
c=1 # the speed of the initial wave
#boundary conditions
u = numpy.ones(nx)
u[int(0.5/dx):int(1/(dx+1))] = 2
un = numpy.ones(nx)
#initializing the velocity function
for i in range(nt):
un= u.copy()
for i in range(1,nx):
u[i]= un[i] -c*(dt/dx)*(u[i]-u[i-1])
pyplot.xlabel('Position')
pyplot.ylabel('Velocity')
pyplot.plot(numpy.linspace(0,2,nx),u)
There are a few things going on here
You don't need to write out the full name of the packages you are importing. You can just use aliasing to call those packages and use them with those aliases later on. This, for example.
import numpy as np
Your dx value initalization will give you 0 beause you are dividing 2 by 40 which will give you a zero. You can initialize the value of dx by making one of the values in that expression a float, so something like this.
dx=float(2)/(nx-1) #width between space intervals
As Meowcolm Law in the comments suggested in the comments, add pyplot.show() to
show the graph. This is what the edited version of your code will like
import numpy as np
import matplotlib.pyplot as plt
import time,sys
#specifying parameters
nx=41 # number of space steps
nt=25 #number of time steps
dt=0.025 #width between time intervals
dx=float(2)/(nx-1) #width between space intervals
c=1 # the speed of the initial wave
#boundary conditions
u = np.ones(nx)
u[int(0.5/dx):int(1/(dx+1))] = 2
un = np.ones(nx)
#initializing the velocity function
for i in range(nt):
un= u.copy()
for i in range(1,nx):
u[i]= un[i] -c*(dt/dx)*(u[i]-u[i-1])
plt.xlabel('Position')
plt.ylabel('Velocity')
plt.plot(np.linspace(0,2,nx),u)
plt.show()
You can add
%matplotlib inline
in order to view the plot inside the notebook. I missed this step following the same guide.
I have been thinking about it for a long time, but I don't find out what the problem is. Hope you can help me, Thank you.
F(s) Gaussian function
F(s)=1/(√2π s) e^(-(w-μ)^2/(2s^2 ))
Code:
import numpy as np
from matplotlib import pyplot as plt
from math import pi
from scipy.fft import fft
def F_S(w, mu, sig):
return (np.exp(-np.power(w-mu, 2)/(2 * np.power(sig, 2))))/(np.power(2*pi, 0.5)*sig)
w=np.linspace(-5,5,100)
plt.plot(w, np.real(np.fft.fft(F_S(w, 0, 1))))
plt.show()
Result:
As was mentioned before you want the absolute value, not the real part.
A minimal example, showing the the re/im , abs/phase spectra.
import numpy as np
import matplotlib.pyplot as p
%matplotlib inline
n=1001 # add 1 to keep the interval a round number when using linspace
t = np.linspace(-5, 5, n ) # presumed to be time
dt=t[1]-t[0] # time resolution
print(f'sampling every {dt:.3f} sec , so at {1/dt:.1f} Sa/sec, max. freq will be {1/2/dt:.1f} Hz')
y = np.exp(-(t**2)/0.01) # signal in time
fr= np.fft.fftshift(np.fft.fftfreq(n, dt)) # shift helps with sorting the frequencies for better plotting
ft=np.fft.fftshift(np.fft.fft(y)) # fftshift only necessary for plotting in sequence
p.figure(figsize=(20,12))
p.subplot(231)
p.plot(t,y,'.-')
p.xlabel('time (secs)')
p.title('signal in time')
p.subplot(232)
p.plot(fr,np.abs(ft), '.-',lw=0.3)
p.xlabel('freq (Hz)')
p.title('spectrum, abs');
p.subplot(233)
p.plot(fr,np.real(ft), '.-',lw=0.3)
p.xlabel('freq (Hz)')
p.title('spectrum, real');
p.subplot(235)
p.plot(fr,np.angle(ft), '.-', lw=0.3)
p.xlabel('freq (Hz)')
p.title('spectrum, phase');
p.subplot(236)
p.plot(fr,np.imag(ft), '.-',lw=0.3)
p.xlabel('freq (Hz)')
p.title('spectrum, imag');
you have to change from time scale to frequency scale
When you make a FFT you will get the simetric tranformation, i.e, mirror of the positive to negative curve. Usually, you only will look at the positive side.
Also, you should take care with sample rate, as FFT is designed to transform time domain input to frequency domain, the time, or sample rate, of input info matters. So add timestep in np.fft.fftfreq(n, d=timestep) for your sample rate.
If you simple want to make a fft of normal dist signal, here is another question with it and some good explanations on why are you geting this behavior:
Fourier transform of a Gaussian is not a Gaussian, but thats wrong! - Python
There are two mistakes in your code:
Don't take the real part, take the absoulte value when plotting.
From the docs:
If A = fft(a, n), then A[0] contains the zero-frequency term (the mean
of the signal), which is always purely real for real inputs. Then
A[1:n/2] contains the positive-frequency terms, and A[n/2+1:] contains
the negative-frequency terms, in order of decreasingly negative
frequency.
You can rearrange the elements with np.fft.fftshift.
The working code:
import numpy as np
from matplotlib import pyplot as plt
from math import pi
from scipy.fftpack import fft, fftshift
def F_S(w, mu, sig):
return (np.exp(-np.power(w-mu, 2)/(2 * np.power(sig, 2))))/(np.power(2*pi, 0.5)*sig)
w=np.linspace(-5,5,100)
plt.plot(w, fftshift(np.abs(np.fft.fft(F_S(w, 0, 1)))))
plt.show()
Also, you might want to consider scaling the x axis too.
I have written one code where I want to plot multiple graphs in a same plot. When I run this, I do not get any result or any error. I am trying to take each value of a and plot graph using the program. So i suppose to have 4 graph as a have 4 elements. Most importantly all graphs shall be in a same figure. Though I am not getting any error or any result I am a bit confuse whether the for loop is working or not. When I use a fixed alpha value and, without using for loop, my code works perfectly. Any idea how to perform that? If my question is not clear please let me know.
import numpy as np
import matplotlib.pyplot as plt
r=10**-6
a=np.array([10,30,60,90])
E=[]
B=[]
gamma_sa=58.6*10**-3
gamma_sw=25*10**-3
gamma_pa=153*10**-3
gamma_pw=110*10**-3
gamma_aw=72.5*10**-3
kt= 1.38*10**-23*290
i=0
for n in a:
alpha=n*(np.pi/180)
while i<360:
beta=(90-i)*(np.pi/180)
if i>=alpha*180/np.pi and i<=180-alpha*180/np.pi:
Energy= 2*np.pi*r**2*(gamma_pw+gamma_sw*np.cos(alpha)+gamma_sa*(1-np.cos(alpha))-0.5*gamma_aw*np.sin(alpha)**2)
elif i>=180+alpha*180/np.pi and i<=360-alpha*180/np.pi:
Energy=2*np.pi*r**2*(gamma_sw+gamma_pw*(np.cos(alpha))+gamma_pa*(1-np.cos(alpha))-0.5*gamma_aw*np.sin(alpha)**2)
elif i>180-alpha*180/np.pi and i<180+alpha*180/np.pi :
x=np.arcsin(((1/np.tan(-beta))*(1/np.tan(alpha))))
w=np.cos(alpha)
y=np.arctan((np.cos(-beta))/(np.sqrt((np.sin(beta)**2)-(np.cos(alpha)**2))))
z= (2*r**2)*((x*w)-y)
A_pa= (np.pi*r**2)*(1-np.cos(alpha))+z
A_sa=(2*np.pi*r**2)*(1-np.cos(alpha))-A_pa
A_sw=(2*np.pi*r**2)-A_sa
A_pw=(2*np.pi*r**2)-A_pa
Energy= (gamma_sa*A_sa)+(gamma_pa*A_pa)+(gamma_sw*A_sw)+(gamma_pw*A_pw)-(np.pi*r**2*gamma_aw*np.sin(alpha)**2)
else:
x=np.arcsin(((1/np.tan(beta))*(1/np.tan(alpha))))
w=np.cos(alpha)
y=np.arctan((np.cos(beta))/(np.sqrt((np.sin(beta)**2)-(np.cos(alpha)**2))))
z= (2*r**2)*((x*w)-y)
A_pa= (np.pi*r**2)*(1-np.cos(alpha))+z
A_sa=(2*np.pi*r**2)*(1-np.cos(alpha))-A_pa
A_sw=(2*np.pi*r**2)-A_sa
A_pw=(2*np.pi*r**2)-A_pa
Energy= (gamma_sa*A_sa)+(gamma_pa*A_pa)+(gamma_sw*A_sw)+(gamma_pw*A_pw)-(np.pi*r**2*gamma_aw*np.sin(alpha)**2)
orientation=i
i=i+0.1
E.append(Energy/kt)
B.append(orientation)
plt.figure(1)
plt.title('Change of surface energy with rotation')
plt.plot(B,E)
plt.grid (axis='both')
plt.xlabel(r'$90^0 $ - $\beta $')
plt.ylabel(r'E($\alpha $)/kT')
plt.show()
The problem is that you are putting the plot command outside the for loop and so the 4 curves will not be plotted. Moreover some of your variables needed to be redefined. Following is the working solution (without the if else statements):
import numpy as np
import matplotlib.pyplot as plt
# r, a, 5 gamma values and kt here
fig = plt.figure(figsize=(8, 6))
for n in a:
i = 0
E=[]
B=[]
alpha=n*(np.pi/180)
while i<360:
# Code here
.
.
.
Energy= (gamma_sa*A_sa)+(gamma_pa*A_pa)+(gamma_sw*A_sw)+(gamma_pw*A_pw)-(np.pi*r**2*gamma_aw*np.sin(alpha)**2)
orientation=i # Mind the indent. Should be outside elif
i=i+0.1
E.append(Energy/kt)
B.append(orientation)
plt.plot(B,E, label='$%d^o$'%n) # Mind the indentation here. Should be outside the while loop
plt.title('Change of surface energy with rotation')
plt.grid (axis='both')
plt.xlabel(r'$90^0 $ - $\beta $')
plt.ylabel(r'E($\alpha $)/kT')
plt.legend()
plt.tight_layout()
Output
I know it is possible to create .wav file from frequency domain data (magnitude + phase) but I would like to know how close would that be to the real(orginal) sound ? Does it depend on the frequency step for example (or something else).
Second question:
I need to write a code that takes a frequency domain data (magnitude + phase) to build a wav file.
In order to do so, I started by the following code which creates a fake signal --> fft (at this point I have the kind of input(mag + phase) that I would expect for my target code). But it doesn't seem top work fine, could you please help
import numpy as np
from scipy import pi
import matplotlib.pyplot as plt
#%matplotlib inline
from scipy.fftpack import fft
min=0
max=400
def calculateFFT (timeStep,micDataX,micDataY):
n=micDataX.size
FFT=np.fft.fft(micDataY)
fft_amlitude=2*abs(FFT)/n
fft_phase=np.angle(FFT)
fft_freq= np.fft.fftfreq(n, d=timeStep) #not used created manually (7 lines) check pi_fFreqDomainCreateConstantBW it is kept here to compare sizes
upper_bound=int((n)/2)
return fft_freq[1:upper_bound],fft_amlitude[1:upper_bound],fft_phase[1:upper_bound]
def calculateI_FFT (n,amplitude_spect,phase_spect):
data=list()
for mag,phase in zip(amplitude_spect,phase_spect):
data.append((mag*n/2)*(np.cos(phase)+1j* np.sin(phase)))
full_data=list(data)
i_data=np.fft.irfft(data)
return i_data
#sampling rate and time vector
start_time=0 #sec
end_time= 2
sampling_rate=1000 #Hz
N=(end_time-start_time)*sampling_rate
#Freq domain peaks
peak1_hz=60 # freq of peak
peak1_mag= 25
peak2_hz=270 # freq of peak
peak2_mag= 2
#Vibration data generation
time =np.linspace(start_time,end_time,N)
vib_data=peak1_mag*np.sin(2*pi*peak1_hz*time)+peak2_mag*np.sin(2*pi*peak2_hz*time)
#Data plotting
plt.plot(time[min:max],vib_data[min:max])
# fft
time_step=1/sampling_rate
fft_freq,fft_data,fft_phase=calculateFFT(time_step,time,vib_data)
#ifft
i_data=calculateI_FFT(N,fft_data,fft_phase)
#plotting
plt.plot(time[min:max],i_data[min:max])
plt.xlabel("Time (s)")
plt.ylabel("Vibration (g)")
plt.title("Time domain")
plt.show()
The output signal screenshot is attached (blue for original signal Orange for the reconstructed one)
enter image description here
Thank you!
I wrote the following code,
import numpy as np
from random import gauss
from random import seed
from pandas import Series
import matplotlib.pyplot as plt
import math
###variable declaration
R=0.000001 #radiaus of particle
beta=0.23 # shape factor
Ad=9.2#characteristic nanoscale defect area
gamma=4*10**-2 #surface tension
tau=.0001 #line tension
phi=-(math.pi/2)#spatial perturbation
lamda=((Ad)/(2*3.14*R)) #averge contcat line position
mu=0.001#viscosity of liquid
lamda_m=10**-9# characteristic size of adsorption site
KbT=(1.38**-24)*293 # boltzman constant with tempartaure
nu=0.001#moleculer volume in liquid phase 1
khi=3 #scaling factor
#deltaF=(beta*gamma*Ad)#surface energy perturbation
deltaF=19*KbT
# seed random number generator
seed(0)
# create white noise series
series = [gauss(0.0, 1.0) for i in range(1)]
series = Series(series)
#########################################
Z=0.0000001 #particle position
time=1
dt=1
for time in np.arange(1, 100, dt):
#####simulation loop#######
theta=np.arccos(-Z/R) #contact angle
theta_e=((math.pi*110)/180) #equilibrium contact angle
Z_e=-R*np.cos(theta_e)#equilibrium position of particle
C=3.14*gamma*(R-Z_e) #additive constant
Fsz= (gamma*math.pi*(Z-Z_e)**2)+(tau*2*math.pi*math.sqrt(R**2-Z**2))+C
Fz=Fsz+(0.5*deltaF*np.sin((2*math.pi/lamda)*(Z-Z_e)-phi))#surface force
#dFz=(((gamma*Ad)/2)*np.sin(2*math.pi/lamda))+((Z-Z_e)*(2*gamma*math.pi))-((tau*2*math.pi*Z)/(math.sqrt(R**2-Z**2)))
dFz=(deltaF*np.sin(2*math.pi/lamda))+((Z-Z_e)*(2*gamma*math.pi))-((tau*2*math.pi*Z)/(math.sqrt(R**2-Z**2)))
w_a=gamma*lamda_m**2*(1-np.cos(theta_e)) #work of adhesion
epsilon_z=2*math.pi*R*np.sin(theta)*mu*(nu/(lamda_m**3))*np.exp(w_a/KbT)#transitional drag
epsilon_s=khi*mu*((4*math.pi**2*R**2)/math.sqrt(Ad))*(1-(Z/R)**2)
epsilon=epsilon_z+epsilon_s
Ft=math.sqrt(2*KbT*epsilon)*series #thermal force
v=(dFz+Ft)/epsilon ##new velocity
Z=Z+v*dt #new position
print('z=',Z)
print('v=',v)
print('Fz=',Fz)
print('dFz',dFz)
print('time',time)
plt.plot(Z,time)
plt.show()
According to my code I suppose to have 99 values for everything (Fz, Z, v , time). While I print , I can see all the values but while I was trying to plot them with different parameters with respect to each other for analyzing, I never get any graph. Can anyone tell me, what is missing from my code with explanation?
#AnttiA's answer is basically correct, but can be easily misunderstood, as can be seen from the OP's comment. Therefore here the complete code altered such that a plot is actually produced. Instead of making Z a list, define another variable as list, say Z_all = [], and then append the updated Z-values to that list. The same can be done for the time variable, i.e. time_all = np.arange(1,100,dt). Finally, take the plot command out of the loop and plot the entire data series at once.
Note that in your example you don't really have a series of random numbers, you pull one fixed number for one fixed seed, thus the plot is not really meaningful (it appears to be producing a straight line). Trying to interpret your intentions correctly, you probably want a series of random numbers that is as long as your time series. This is most easily done using np.random.normal
There are a lot of other ways that your code could be optimised. For instance, all mathematical functions from the math module are also found in numpy, so you could just not import math at all. The same goes for pandas. Also, you define some constant values inside the for-loop, which could be computed once before the loop. Lastly, #AnttiA is probably right, that you want time on the x axis and Z on the y axis. I therefore generate two plots -- on the left time over Z and on the right Z over time. Now finally the altered code:
import numpy as np
#from random import gauss
#from random import seed
#from pandas import Series
import matplotlib.pyplot as plt
#import math
###variable declaration
R=0.000001 #radiaus of particle
beta=0.23 # shape factor
Ad=9.2#characteristic nanoscale defect area
gamma=4*10**-2 #surface tension
tau=.0001 #line tension
phi=-(np.pi/2)#spatial perturbation
lamda=((Ad)/(2*3.14*R)) #averge contcat line position
mu=0.001#viscosity of liquid
lamda_m=10**-9# characteristic size of adsorption site
KbT=(1.38**-24)*293 # boltzman constant with tempartaure
nu=0.001#moleculer volume in liquid phase 1
khi=3 #scaling factor
#deltaF=(beta*gamma*Ad)#surface energy perturbation
deltaF=19*KbT
##quantities moved out of the for-loop:
theta_e=((np.pi*110)/180) #equilibrium contact angle
Z_e=-R*np.cos(theta_e)#equilibrium position of particle
C=3.14*gamma*(R-Z_e) #additive constant
w_a=gamma*lamda_m**2*(1-np.cos(theta_e)) #work of adhesion
#########################################
Z=0.0000001 #particle position
##time=1
dt=1
Z_all = []
time_all = np.arange(1, 100, dt)
# seed random number generator
# seed(0)
np.random.seed(0)
# create white noise series
##series = [gauss(0.0, 1.0) for i in range(1)]
##series = Series(series)
series = np.random.normal(0.0, 1.0, len(time_all))
for time, S in zip(time_all,series):
#####simulation loop#######
Z_all.append(Z)
theta=np.arccos(-Z/R) #contact angle
Fsz= (gamma*np.pi*(Z-Z_e)**2)+(tau*2*np.pi*np.sqrt(R**2-Z**2))+C
Fz=Fsz+(0.5*deltaF*np.sin((2*np.pi/lamda)*(Z-Z_e)-phi))#surface force
#dFz=(((gamma*Ad)/2)*np.sin(2*np.pi/lamda))+((Z-Z_e)*(2*gamma*np.pi))-((tau*2*np.pi*Z)/(np.sqrt(R**2-Z**2)))
dFz=(deltaF*np.sin(2*np.pi/lamda))+((Z-Z_e)*(2*gamma*np.pi))-((tau*2*np.pi*Z)/(np.sqrt(R**2-Z**2)))
epsilon_z=2*np.pi*R*np.sin(theta)*mu*(nu/(lamda_m**3))*np.exp(w_a/KbT)#transitional drag
epsilon_s=khi*mu*((4*np.pi**2*R**2)/np.sqrt(Ad))*(1-(Z/R)**2)
epsilon=epsilon_z+epsilon_s
Ft=np.sqrt(2*KbT*epsilon)*S #series #thermal force
v=(dFz+Ft)/epsilon ##new velocity
Z=Z+v*dt #new position
print('z=',Z)
print('v=',v)
print('Fz=',Fz)
print('dFz',dFz)
print('time',time)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8,4))
axes[0].plot(Z_all,time_all)
axes[0].set_xlabel('Z')
axes[0].set_ylabel('t')
axes[1].plot(time_all, Z_all)
axes[1].set_xlabel('t')
axes[1].set_ylabel('Z')
fig.tight_layout()
plt.show()
The result looks like this:
I suppose, you will get plot anyway, y-values are probably from 94 to 104.
Now you are plotting line with one point. Its length is zero, that's why you cannot see it, try: plt.plot(Z,time,'*').
Now you should get graph with an asterix in the middle.
As Thomas suggested, you should use arrays instead of using last calculated value. If you prefer loops (sometimes they are easier to modify), modify the lines...
Before loop:
Z = [0.0000001] # Initialize Z for time 0
time_vec = np.arange(1, 100, dt)
Inside loop:
Z.append(Z[-1] + v*dt) # new position
After loop:
plt.plot(Z[1:], time_vec)
Have no time to test it, hopefully works...
Note that first argument in plot command is x-axis values and second y-axis, I'd prefer time in x-axis.