0% found this document useful (0 votes)
39 views10 pages

Save and Read Data

The document demonstrates how to perform 1D, 2D, and 3D inversion using Python. It generates synthetic data, adds noise, and inverts it using Fourier transforms with different kernels. Plots are generated to visualize the results. Real data can be read from a file and inverted.

Uploaded by

Miftahul Jannah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

Save and Read Data

The document demonstrates how to perform 1D, 2D, and 3D inversion using Python. It generates synthetic data, adds noise, and inverts it using Fourier transforms with different kernels. Plots are generated to visualize the results. Real data can be read from a file and inverted.

Uploaded by

Miftahul Jannah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/26/23, 1:19 PM Inversi 2.

0 - Jupyter Notebook

In [1]: 

import numpy as np

# Define the dimensions of the 3D grid


width = 5
height = 5
depth = 5

# Create an empty 3D grid (all zeros)


grid = np.zeros((width, height, depth), dtype=int)

# Set some voxels to 1 to represent the object


grid[2:4, 2:4, 1:3] = 1

# Print the 3D grid (0 for empty, 1 for object)


for z in range(depth):
print(f"Layer {z}:")
for y in range(height):
row = ""
for x in range(width):
row += str(grid[x, y, z])
print(row)

# You can visualize or process the 3D grid further as needed.

Layer 0:
00000
00000
00000
00000
00000
Layer 1:
00000
00000
00110
00110
00000
Layer 2:
00000
00000
00110
00110
00000
Layer 3:
00000
00000
00000
00000
00000
Layer 4:
00000
00000
00000
00000
00000

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 1/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

In [2]: 

import matplotlib.pyplot as plt


from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generate example data (replace this with your inversion results)


x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the surface


surface = ax.plot_surface(X, Y, Z, cmap='viridis')

# Add color bar which maps values to colors


fig.colorbar(surface, shrink=0.5, aspect=5)

# Set labels and title


ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.set_title('3D Inversion Plot')

# Show the plot


plt.show()

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 2/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

In [5]: 

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define the grid size and extent


grid_size = 50
x = np.linspace(-5, 5, grid_size)
y = np.linspace(-5, 5, grid_size)

# Create a 2D grid (remove Z)


X, Y = np.meshgrid(x, y)

# Define the target function (you can replace this with your actual data)
target_function = np.exp(-(X**2 + Y**2))

# Create a kernel matrix (for this example, a simple Gaussian kernel)


kernel = np.exp(-(X**2 + Y**2))

# Perform the inversion using the kernel matrix


inversion_result = np.fft.ifftn(np.fft.fftn(target_function) / np.fft.fftn(kernel))

# Plot the results in 3D


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_title('3D Inversion Result')
ax.plot_surface(X, Y, np.real(inversion_result), cmap='viridis', alpha=0.7)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Inversion Result')
ax.view_init(azim=45, elev=30) # Adjust the view angle

plt.show()

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 3/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

In [6]: 

import numpy as np
import matplotlib.pyplot as plt

# Define the grid size and extent


grid_size = 100
x = np.linspace(-5, 5, grid_size)

# Define the target function (you can replace this with your actual data)
target_function = np.exp(-x**2)

# Create a kernel matrix (for this example, a simple Gaussian kernel)


kernel = np.exp(-x**2)

# Perform the inversion using the kernel matrix


inversion_result = np.fft.ifft(np.fft.fft(target_function) / np.fft.fft(kernel))

# Plot the results


plt.figure()
plt.title('1D Inversion Result')
plt.plot(x, target_function, label='Target Function')
plt.plot(x, np.real(inversion_result), label='Inversion Result', linestyle='--')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.grid(True)
plt.show()

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 4/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

In [7]: 

import numpy as np
import matplotlib.pyplot as plt
import random as rnd

a = 2
b = 0.1
c = 0.1
d = 0.0015
e = 0.00001

x = np.array(range(-10, 10))
y = a + b*(x) + c*(x**2) + d*(x**3) + e*(x**4)
noise = np.random.randn()
yn = y + noise

plt.plot(x, y,'b-')
plt.plot(x, yn,'ro')
plt.xlabel("x")
plt.ylabel("y")
plt.show()

print(y)
print(yn)

[ 9.6 8.17211 6.87296 5.70951 4.68896 3.81875 3.10656 2.56031


2.18816 1.99851 2. 2.20151 2.61216 3.24131 4.09856 5.19375
6.53696 8.13851 10.00896 12.15911]
[10.68932033 9.26143033 7.96228033 6.79883033 5.77828033 4.90807033
4.19588033 3.64963033 3.27748033 3.08783033 3.08932033 3.29083033
3.70148033 4.33063033 5.18788033 6.28307033 7.62628033 9.22783033
11.09828033 13.24843033]

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 5/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

In [12]: 

import numpy as np

# Pendefinisikan variabel a, b, c, d, dan e serta hasil perhitungannya


a = 2
b = 0.1
c = 0.1
d = 0.0015
e = 0.00001

x = np.array(range(-10, 11))
y = a + b*(x) + c*(x**2) + d*(x**3) + e*(x**4)

# Penambahan noise ke y
noise = np.random.randn(len(x))
yn = y + noise

# Penampilan plot
import matplotlib.pyplot as plt

plt.plot(x, y, 'b-')
plt.plot(x, yn, 'ro')
plt.xlabel("x")
plt.ylabel("y")
plt.show()

print(y)
print(yn)

# Penyimpan data x dan yn ke dalam file teks


with open('Data Inversi 1.txt', 'w') as file:
file.write("x, yn\n") # Header
for i in range(len(x)):
file.write(f'{x[i]}, {yn[i]}\n')
print("Data telah ditulis ke hasil_kalkulasi.txt")

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 6/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

[ 9.6 8.17211 6.87296 5.70951 4.68896 3.81875 3.10656 2.56031


2.18816 1.99851 2. 2.20151 2.61216 3.24131 4.09856 5.19375
6.53696 8.13851 10.00896 12.15911 14.6 ]
[10.80994065 5.76096869 6.42715272 7.19331172 5.35758859 4.44636351
2.6508871 1.94890462 2.73008978 2.62252087 0.92565954 2.02172666
1.82769402 3.75364851 5.2856416 5.83585734 7.00235352 7.78666961
10.10781187 11.98886644 13.16788494]
Data telah ditulis ke hasil_kalkulasi.txt

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 7/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

In [13]: 

# Pengunggahan Pustaka yang Digunakan


import numpy as np
import matplotlib.pyplot as plt

# Pendefinisikan variabel a, b, c, d, dan e


a = 2
b = 0.1
c = 0.1
d = 0.0015
e = 0.00001

x = np.array(range(-10, 11))
y = a + b*(x) + c*(x**2) + d*(x**3) + e*(x**4)

# Penambahan noise ke y
noise = np.random.randn(len(x))
yn = y + noise

# Penampilan plot
import matplotlib.pyplot as plt

plt.plot(x, y, 'b-')
plt.plot(x, yn, 'ro')
plt.xlabel("x")
plt.ylabel("y")
plt.show()

print(y)
print(yn)

# Penyimpan data x dan yn ke dalam file teks


with open('Data Inversi 1.txt', 'w') as file:
file.write("x, yn\n") # Header
for i in range(len(x)):
file.write(f'{x[i]}, {yn[i]}\n')
print("Data telah ditulis ke hasil_kalkulasi.txt")

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 8/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

[ 9.6 8.17211 6.87296 5.70951 4.68896 3.81875 3.10656 2.56031


2.18816 1.99851 2. 2.20151 2.61216 3.24131 4.09856 5.19375
6.53696 8.13851 10.00896 12.15911 14.6 ]
[ 9.95229939 8.42621648 8.05528631 5.29296671 4.86440357 5.02069609
In3.85175919
[16]: 3.61904796 3.87714327 3.22507962 1.98666062 2.74382946 

# Pengunggahan 2.94167631
3.25994276 5.15481309
Pustaka yang Digunakan 5.63746767 5.66380992 8.6802462
7.45591694
import 13.09368824
numpy as np 12.28156775]
Data telah ditulis ke hasil_kalkulasi.txt
import matplotlib.pyplot as plt

# Pembacaan Data dari File txt


data = []

with open(r'C:\Users\mjann\Dropbox\My PC (DESKTOP-M2TMC1N)\Downloads\MT\Data Inversi 1.tx


header = file.readline() # Baca baris pertama sebagai header (biasanya mengandung n
for line in file:
x_str, yn_str = line.strip().split(', ') # Membagi baris menjadi kolom x dan yn
x = float(x_str)
yn = float(yn_str)
data.append((x, yn))

# Pemisahan Data x dan yn dari Daftar Data


x = [item[0] for item in data]
yn = [item[1] for item in data]

G = [np.ones(20), x, x**2, x**3, x**4]


Gt = np.transpose(G) #G sudah tertranspose

m = np.array([a,b,c,d,e])

y = np.matmul(Gt,m)

# Penampilan Data
for i in range(len(x)):
plt.plot(x, y, 'bo')
plt.show()

Data:

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 9/10


9/26/23, 1:19 PM Inversi 2.0 - Jupyter Notebook

In [ ]: 

localhost:8888/notebooks/Downloads/MT/Inversi 2.0.ipynb 10/10

You might also like