Uploaded by Danieltran

IN3190 project danietr H23

advertisement
IN3190 - Mandatory Project H23
University of Oslo
Daniel Tran
19. October 2023
Mandatory Project H23 - IN3190
UiO
Daniel Tran
Task 1A
Figur 1: Map Plot
Task 1B
Code:
1
2
3
4
5
6
7
8
9
% Task 1B
% F i n d t h e s h o r t e s t and l o n g e s t g r e a t c i r c l e
D = c o n v l e n g t h ( D , 'm ' , ' km ' ) ;
s h o r t e s t _ d i s t a n c e = min ( D ) ;
l o n g e s t _ d i s t a n c e = max( D ) ;
% Display the results
d i s p ( [ ' S h o r t e s t Great C i r c l e D i s t a n c e :
d i s p ( [ ' Longest Great C i r c l e D i s t a n c e :
'
'
num2str ( shortest_distance ) ' kilometers ' ] ) ;
num2str ( longest_distance ) ' kilometers ' ] ) ;
Output:
Shortest Great Circle Distance: 2132.9599 kilometers
Longest Great Circle Distance: 17682.7856 kilometers
1 of 11
Mandatory Project H23 - IN3190
UiO
Daniel Tran
Task 1C
Code:
1
2
3
4
5
6
7
8
9
%%
% Task 1C
% C r e a t e a p l o t to d i s p l a y t h e s o r t e d d i s t a n c e s
figure ;
p l o t ( s o r t e d _ s o r t e d _ d i s t a n c e s , ' o= ' ) ; % P l o t s o r t e d d i s t a n c e s as a l i n e w i t h c i r c l e s
t i t l e ( ' S o r t e d Great C i r c l e D i s t a n c e s between Hunga Tonga and S t a t i o n s ' ) ;
x l a b e l ( ' S t a t i o n Index ' ) ;
y l a b e l ( ' Great C i r c l e D i s t a n c e ( m e t e r s ) ' ) ;
g r i d on ;
Plot:
Figur 2: Display of sorted distances between event and station
Task 2A
Code:
1
2
3
4
5
%%
% Task 2A
n = 0 : l e n g t h ( h1 )
=
1;
figure ;
2 of 11
Mandatory Project H23 - IN3190
6
7
8
9
10
11
12
13
14
15
16
17
18
UiO
subplot (3 , 1 , 1) ;
s t e m ( n , h1 ) ;
t i t l e ( ' FIR f i l t e r i m p u l s e r e s p o n e s h1 ' )
subplot (3 , 1 , 2) ;
s t e m ( n , h2 ) ;
t i t l e ( ' FIR f i l t e r i m p u l s e r e s p o n e s h2 ' )
subplot (3 , 1 , 3) ;
s t e m ( n , h3 ) ;
t i t l e ( ' FIR f i l t e r i m p u l s e r e s p o n e s h3 ' )
s g t i t l e ( ' FIR F i l t e r Impulse Responses ' ) ;
Plot:
Figur 3: Plot of FIR Filter impulse respones
Task 2B
Code:
1
2
3
4
5
6
7
8
%%
% Task 2B
xtest = [ 1 , 2 , 3 , 4 , 5 ] ;
htest = [10 , 9 ] ;
c = conv ( htest , xtest ) ;
disp ( c )
test = c o n v o l u t i o n _ 2 b ( htest , xtest , 1) ;
disp ( test )
3 of 11
Daniel Tran
Mandatory Project H23 - IN3190
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
UiO
Daniel Tran
function y = convolution_2b (h , x , ylen_choice )
M = length ( x ) ;
N = length ( h ) ;
i f y l e n _ c h o i c e == 1
ylen = M + N = 1 ;
e l s e i f y l e n _ c h o i c e == 0
ylen = M ;
else
e r r o r ( " Choice e i t h e r 0 o r 1 " ) ;
end
y = zeros (1 , ylen ) ;
f o r n = 1 : ylen
f o r k = max ( 1 , n = N +1) : min ( M , n )
y ( n ) = y ( n ) + x ( k ) * h ( n = k + 1) ;
end
end
end
Output to verify that my function works:
10 29 48 67 86 45
10 29 48 67 86 45
Task 2C
Code:
1
2
3
4
5
6
7
8
9
10
11
12
%%
% Task 2C
f u n c t i o n [ X _ D T F T , f ] = D T F T 2 c ( x , N , fs )
X_DTFT = zeros (1 , N ) ;
o m e g a = l i n s p a c e ( 0 , 2 * pi , N ) ;
f = o m e g a * fs / ( 2 * pi ) ;
for k = 1: N
f o r n = 1: length ( x )
X_DTFT ( k ) = X_DTFT ( k ) + x ( n )
end
end
end
*
e x p ( =1 j
Task 2D
Code:
1
2
3
4
5
6
7
8
9
10
11
%%
% Task 2D
N = 256;
fs = 1 0 0 0 ;
% D T F T f o r h1
[ X1 , f ] = D T F T 2 c ( h1 , N , fs ) ;
% D T F T f o r h2
[ X2 , ~ ] = D T F T 2 c ( h2 , N , fs ) ;
4 of 11
*
omega ( k )
*
n) ;
Mandatory Project H23 - IN3190
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
UiO
Daniel Tran
% D T F T f o r h3
[ X3 , ~ ] = D T F T 2 c ( h3 , N , fs ) ;
% P l o t t h e a b s o l u t e v a l u e of t h e D T F T f o r h1
figure ;
subplot (3 , 1 , 1) ;
p l o t ( f , abs ( X1 ) ) ;
x l a b e l ( ' Frequency ( Hz ) ' ) ;
y l a b e l ( ' Magnitude ' ) ;
t i t l e ( ' A b s o l u t e Value o f DTFT f o r h1 ( n ) ' ) ;
x l i m ( [ 0 , fs / 2 ] ) ;
% P l o t t h e a b s o l u t e v a l u e of t h e D T F T f o r h2
subplot (3 , 1 , 2) ;
p l o t ( f , abs ( X2 ) ) ;
x l a b e l ( ' Frequency ( Hz ) ' ) ;
y l a b e l ( ' Magnitude ' ) ;
t i t l e ( ' A b s o l u t e Value o f DTFT f o r h2 ( n ) ' ) ;
x l i m ( [ 0 , fs / 2 ] ) ;
% P l o t t h e a b s o l u t e v a l u e of t h e D T F T f o r h3
subplot (3 , 1 , 3) ;
p l o t ( f , abs ( X3 ) ) ;
x l a b e l ( ' Frequency ( Hz ) ' ) ;
y l a b e l ( ' Magnitude ' ) ;
t i t l e ( ' A b s o l u t e Value o f DTFT f o r h3 ( n ) ' ) ;
x l i m ( [ 0 , fs / 2 ] ) ;
Plot:
Figur 4: Absolute value of the frequency spectrum of the provided impulse responses h1(n), h2(n),
h3(n)
5 of 11
Mandatory Project H23 - IN3190
UiO
Daniel Tran
Task 2E
From Task 2D we can see that h1 (n) is a lowpass lter, h2 (n) is a bandpass lter and h3 (n) is a
highpass lter
Task 2F
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%%
%T a s k 2 F
filtered_data_collection_h1 = cell ( N_files , 1) ;
filtered_data_collection_h2 = cell ( N_files , 1) ;
filtered_data_collection_h3 = cell ( N_files , 1) ;
f o r ii = 1 : N _ f i l e s
d a t a = d a t a _ c o l l e c t i o n { ii } ;
end
% A p p l y e a c h f i l t e r to t h e d a t a
f i l t e r e d _ d a t a _ c o l l e c t i o n _ h 1 { ii } = c o n v ( data , h1 ,
f i l t e r e d _ d a t a _ c o l l e c t i o n _ h 2 { ii } = c o n v ( data , h2 ,
f i l t e r e d _ d a t a _ c o l l e c t i o n _ h 3 { ii } = c o n v ( data , h3 ,
same ' ) ;
same ' ) ;
' same ' ) ;
'
'
Task 2G
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%T a s k 2 G
figure ;
h o l d on ;
for i = 1: N_files
data = filtered_data_collection_h2 {i } ;
s c a l i n g = 250/max( d a t a ) ;
y = filtered_data_collection_h2 {i }.* scaling + D ( i ) ;
x = times_collection {i };
p l o t ( x , y , L i n e W i d t h =0.01) ;
end
% S e t up p l o t a x e s
colororder ( ' black ' ) ;
x l a b e l ( ' Time ' ) ;
y l a b e l ( ' D i s t a n c e (km) ' ) ;
title ( ' Section Plot ' ) ;
g r i d on ;
hold off ;
6 of 11
Mandatory Project H23 - IN3190
UiO
Daniel Tran
Plot:
I choose lter h2 (n) because it is a bandpass lter, which means it will only let through the frequencies that are in the infrasound range. The other lters will let through frequencies that are not
in the infrasound range, which will make the plot look messy.
Figur 5: Infrasound plot
Task 3A
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%%
% Task 3A
% I n i t i a l i z e a v e c t o r to s t o r e t h e s e l e c t e d a r r i v a l t i m e s
selected_times = zeros ( length ( filtered_data_collection_h1 ) , 1) ;
% Loop over a l l station signals
f o r i = 1: length ( filtered_data_collection_h1 )
% Open a new figure window
figure ;
% P l o t f o u r s u b p l o t s : t h e r a w t r a c e , t h e t r a c e f i l t e r e d by h1 ( n ) , t h e t r a c e f i l t e r e d ←by h2 ( n ) , and t h e t r a c e f i l t e r e d by h3 ( n )
subplot (4 , 1 , 1) ;
plot ( times_collection { sorted_distances ( i ) } , data_collection { sorted_distances ( i ) }) ;
t i t l e ( ' Raw Trace ' ) ;
subplot (4 , 1 , 2) ;
7 of 11
Mandatory Project H23 - IN3190
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
UiO
p l o t ( t i m e s _ c o l l e c t i o n { s o r t e d _ d i s t a n c e s ( i ) } , f i l t e r e d _ d a t a _ c o l l e c t i o n _ h 1 { ←sorted_distances ( i ) }) ;
t i t l e ( ' Trace F i l t e r e d by h1 ( n ) ' ) ;
subplot (4 , 1 , 3) ;
p l o t ( t i m e s _ c o l l e c t i o n { s o r t e d _ d i s t a n c e s ( i ) } , f i l t e r e d _ d a t a _ c o l l e c t i o n _ h 2 { ←sorted_distances ( i ) }) ;
t i t l e ( ' Trace F i l t e r e d by h2 ( n ) ' ) ;
subplot (4 , 1 , 4) ;
p l o t ( t i m e s _ c o l l e c t i o n { s o r t e d _ d i s t a n c e s ( i ) } , f i l t e r e d _ d a t a _ c o l l e c t i o n _ h 3 { ←sorted_distances ( i ) }) ;
t i t l e ( ' Trace F i l t e r e d by h3 ( n ) ' ) ;
% A c t i v a t e a p o i n t e r f o r t h e u s e r to m a n u a l l y s e l e c t t h e a r r i v a l t i m e of t h e h i g h e r =←frequency infrasound wave
selected_time = ginput (1) ;
selected_times ( i ) = selected_time (1) ;
% Close the figure window
close ;
end
selected_times = selected_times *20*60*60;
% S a v e t h e v e c t o r of s e l e c t e d a r r i v a l t i m e s i n a f i l e
%s a v e ( ' s e l e c t e d _ t i m e s . mat ' , ' s e l e c t e d _ t i m e s ' ) ;
Task 3B
Code:
1
2
3
4
5
Daniel Tran
%%
% Task 3B
figure ;
plot ( selected_times , sorted_sorted_distances , "o" ) ;
t i t l e ( ' Estimated t r a v e l time and d i s t a n c e ' ) ;
Plot:
8 of 11
Mandatory Project H23 - IN3190
UiO
Figur 6: Estimated travel time and distance
Task 3C
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
%%
% Task 3C
selected_times_wn = [ ] ;
selected_noisy = [ ] ;
distances_wn = [ ] ;
distances_noisy = [ ] ;
[ p , SS ] = p o l y f i t ( s e l e c t e d _ t i m e s , s o r t e d _ s o r t e d _ d i s t a n c e s , 2 ) ;
for i = 1: N_files
[ y _ p o l y , d e l t a ] = p o l y v a l ( p , s e l e c t e d _ t i m e s ( i ) , SS ) ;
noisy_stations = rmse ( sorted_sorted_distances ( i ) , y_poly ) ;
i f n o i s y _ s t a t i o n s < 1000000
selected_times_wn = [ selected_times_wn selected_times ( i ) ] ;
distances_wn = [ distances_wn sorted_sorted_distances ( i ) ] ;
else
selected_noisy = [ selected_noisy selected_times ( i ) ] ;
distances_noisy = [ distances_noisy sorted_sorted_distances ( i ) ] ;
end
end
figure ;
p l o t ( s e l e c t e d _ n o i s y , d i s t a n c e s _ n o i s y , " o " , " MarkerEdgeColor " , " r " ) ;
h o l d on
p l o t ( s e l e c t e d _ t i m e s _ w n , d i s t a n c e s _ w n , " o " , " MarkerEdgeColor " , "b" ) ;
hold off
9 of 11
Daniel Tran
Mandatory Project H23 - IN3190
UiO
Plot:
Figur 7: Estimated travel time and distance with marked noisy stations
Task 3D
Code:
1
2
3
4
5
6
7
8
9
10
%%
% Task 3D
[ celerity_estimate , S ] = polyfit ( selected_times_wn , distances_wn , 1) ;
[ ce_p , d e l t a x 2 ] = p o l y v a l ( c e l e r i t y _ e s t i m a t e , s e l e c t e d _ t i m e s _ w n , S ) ;
figure ;
p l o t ( s e l e c t e d _ t i m e s _ w n , ce_p , " r " ) ;
h o l d on
p l o t ( s e l e c t e d _ t i m e s _ w n , d i s t a n c e s _ w n , " o " , " MarkerEdgeColor " , "b" ) ;
hold off
Plot:
10 of 11
Daniel Tran
Mandatory Project H23 - IN3190
UiO
Figur 8: Time and distance plot, compared to the estimated celerity
11 of 11
Daniel Tran
Download