-
Notifications
You must be signed in to change notification settings - Fork 0
/
Speed_Calculation.m
1272 lines (988 loc) · 46.1 KB
/
Speed_Calculation.m
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% NEW PROGRAM : Parameters and Setup
% Start timing the entire process
total_time = tic;
% Set path and load images
path = 'C:\Users\admin\Documents\GitHub\Stratocu-Waves-Jr\DATA\2022_09_06\';
image_files = dir(fullfile(path, '*9_06*png'));
images = cell(length(image_files), 1);
% Read and process images
for i = 1:length(image_files)
fname = fullfile(path, image_files(i).name);
img = imread(fname);
red = flipud(img(:, 400:end, 1));
images{i} = red;
end
% Get image dimensions
[height, width] = size(images{1});
x = 0:(width - 1);
y = 0:(height - 1);
[X, Y] = meshgrid(x, y);
% Pixel size
DX = 2000; % meters per pixel
pixel_size_km = DX / 1000; % kilometers per pixel
% Coordinates in meters
Xm = (X - mean(X(:))) * DX;
%Ym = (Y - mean(Y(:))) * DX;
Ym = (-Y + mean(Y(:))) * DX;
%%
% Wave parameters (first wave)
cphase = 15; % m/s
wavelength = 150e3; % meters
direction = 235; % degrees
zamplitude = 100; % meters
PBLdepth = 1000; % m
% Wavenumbers
%k = (2 * pi / wavelength) * sin(direction * pi / 180);
%l = (2 * pi / wavelength) * cos(direction * pi / 180);
% Corrected wave numbers
k = (2 * pi / wavelength) * cos(direction * pi / 180); % Now uses cosine
l = (2 * pi / wavelength) * sin(direction * pi / 180); % Now uses sine
% Amplitude modulation (first wave)
packet_center_x = -400e3;
packet_center_y = -400e3;
packet_width_x = 400e3;
packet_width_y = 300e3;
Ampwindow = exp(-(((Xm - packet_center_x) / packet_width_x).^2 + ...
((Ym - packet_center_y) / packet_width_y).^2));
% Time parameters
time_steps = length(images);
time_resolution = 1800; % seconds between frames
% Initialize the grid to store the synthetic images
grid = zeros(height, width, time_steps);
% Compute angular frequency
omega = cphase * (2 * pi / wavelength);
% Corrected phase calculation
for it = 1:time_steps
t = (it - 1) * time_resolution;
% Corrected phase
phase = k * Xm + l * Ym - omega * t;
% Vertical displacement
dz = zamplitude * sin(phase) .* Ampwindow;
% Horizontal displacements for the first wave
dxy = (zamplitude / PBLdepth) * wavelength * sin(phase - pi / 2) / DX;
% Corrected dx and dy calculations
dx = dxy .* cos(direction * pi / 180) .* Ampwindow; % uses cosine
dy = dxy .* sin(direction * pi / 180) .* Ampwindow; % uses sine
% Total displacements
total_dx = dx;
total_dy = dy;
% Get the current image
img = double(images{it});
% Coordinates for interpolation
XI = X - total_dx;
YI = Y - total_dy;
% Handle boundaries
XI = max(min(XI, width), 1);
YI = max(min(YI, height), 1);
% Warp the image
warped_img = interp2(X, Y, img, XI, YI, 'linear', 0);
% Modulate brightness
modulated_img = warped_img .* (1 + dz / PBLdepth * 5);
% Store the synthetic image
grid(:, :, it) = modulated_img;
end
%%
% Specify the number of frames to process
% Set to Inf to process all frames, or specify a number, e.g., 10 for the first 10 frames
num_frames = 2;
% Get a list of NetCDF files in the directory
nc_files = dir(fullfile(path, '*9_06*png'));
% Adjust num_frames based on available files
if num_frames == Inf
num_frames = numel(nc_files); % Process all frames
else
num_frames = min(num_frames, numel(nc_files)); % Process up to the specified limit
end
% Check if there are enough files to process
if num_frames < 2
error('At least two files are required for processing.');
end
% Shrinking factor
shrinkfactor = 5;
invshrinkfactor = 1 / shrinkfactor;
% Dynamic pixel size (km per pixel)
original_pixel_size_km = 2; % Original pixel size before shrinking
pixel_size_km = original_pixel_size_km * shrinkfactor; % Adjusted pixel size due to shrinking
% Square size in degrees
square_size_deg = 5; % 5x5 degrees squares
% Conversion factor: 1 degree ≈ 111.32 km on Earth's surface
km_per_degree = 111.32;
% Calculate square size in km
square_size_km = square_size_deg * km_per_degree; % Total km per square
% Calculate square size in pixels
square_size_px = round(square_size_km / pixel_size_km);
% Brightness thresholds (adjust as needed)
brightness_threshold = 0.00001; % Mean brightness below which squares are ignored
std_threshold = 10; % Standard deviation above which squares are ignored
% Wavelet transform parameters
NANGLES = 24;
Angles = 0:pi/(NANGLES-1):pi;
% Define scales in km (10 km to 500 km range), but divided by 2 because
% scales are half the wavelength
min_scale_km = 10;
max_scale_km = 100; %500;
NSCALES=20;
Scales_km = logspace(log10(min_scale_km), log10(max_scale_km), NSCALES);
% Convert scales to pixels (independent of shrinkfactor)
Scales = Scales_km / pixel_size_km;
% Windowing parameters (independent of shrinkfactor)
window_buffer = 10; % Buffer size in pixels around the frame edges
% Preprocessing flag
Preprocess_Flag = 1; % 1 is on / 0 is off
% Time interval between frames (adjust if necessary)
time_interval = 1800; % Assuming 30 minutes in seconds
% Metadata saving flag
Save_Metadata_Flag = 1; % 1 to save metadata, 0 to skip
%% Read Data and Initialize
% Read the first file to get dimensions
frame1 = grid(:, :, 1);
% Resize the frame according to the shrink factor if needed
if shrinkfactor ~= 1
frame1 = imresize(frame1, invshrinkfactor);
end
[frame_height, frame_width] = size(frame1);
% Adjust frame dimensions by removing the buffer
x_buffer_range = (window_buffer + 1):(frame_width - window_buffer);
y_buffer_range = (window_buffer + 1):(frame_height - window_buffer);
adjusted_frame_width = length(x_buffer_range);
adjusted_frame_height = length(y_buffer_range);
% Calculate number of squares along width and height
num_squares_x = ceil(adjusted_frame_width / square_size_px);
num_squares_y = ceil(adjusted_frame_height / square_size_px);
% Generate Squares Coordinates
% Generate coordinates for squares
squares = [];
idx = 1;
for i = 1:num_squares_y
for j = 1:num_squares_x
% Calculate pixel indices
x_start = floor((j - 1) * adjusted_frame_width / num_squares_x) + 1;
y_start = floor((i - 1) * adjusted_frame_height / num_squares_y) + 1;
x_end = floor(j * adjusted_frame_width / num_squares_x);
y_end = floor(i * adjusted_frame_height / num_squares_y);
% Ensure valid ranges
if x_end > x_start && y_end > y_start
squares(idx).x_range = x_buffer_range(x_start:x_end);
squares(idx).y_range = y_buffer_range(y_start:y_end);
squares(idx).index = idx;
idx = idx + 1;
end
end
end
% Display Frame 1 with Squares
figure;
% Preprocess frames if needed
if Preprocess_Flag==1
frame1 = preprocess_img(frame1);
end
% Define the parameters of the radial window
radius_factor = 1.1; % 110% of the image radius (to keep the whole image)
decay_rate = 0.05; % Control parameter for decay rate
% Apply windowing only at the edges
frame1_windowed = apply_radial_window(frame1, radius_factor, decay_rate);
imshow(frame1_windowed, [0,1]);
title('Windowed Frame 1');
colormap('gray');
hold on;
for idx = 1:length(squares)
rectangle('Position', [squares(idx).x_range(1), squares(idx).y_range(1), ...
length(squares(idx).x_range), length(squares(idx).y_range)], ...
'EdgeColor', 'r', 'LineWidth', 1);
% Label the square
text(squares(idx).x_range(1), squares(idx).y_range(1) - 5, ...
sprintf('%d', squares(idx).index), 'Color', 'yellow', 'FontSize', 8);
end
title('Frame 1 with Squares');
hold off;
%% Process Frames and Squares
% Adjusted number of entries based on the number of scales
total_entries = (num_frames - 1) * length(squares) * NSCALES;
% Initialize peak_data: columns are [frame_idx, square_idx, scale, angle, phase_difference, speed, coherence_value, wavelength_km]
peak_data = NaN(total_entries, 8); % Now 8 columns to include wavelength
% Initialize a counter for the number of entries
entry_counter = 0;
% Initialize a structure array to store metadata if needed
if Save_Metadata_Flag
square_metadata = struct('frame_idx', {}, 'square_idx', {}, 'spec1', {});
end
% Loop over files (frames)
for frame_idx = 1:num_frames - 1
fprintf('Processing frame %d/%d...\n', frame_idx, num_frames);
% Measure time for processing each frame (optional)
frame_time = tic; % Start timing for a specific frame
% Get frames from synthetic data
frame1 = grid(:, :, frame_idx);
frame2 = grid(:, :, frame_idx + 1);
% Convert to double for processing
frame1 = double(frame1);
frame2 = double(frame2);
% Resize frames according to shrink factor if needed
if shrinkfactor ~= 1
frame1 = imresize(frame1, invshrinkfactor);
frame2 = imresize(frame2, invshrinkfactor);
end
% Preprocess frames if needed
if Preprocess_Flag
frame1 = preprocess_img(frame1);
frame2 = preprocess_img(frame2);
end
% Apply windowing to the entire frame
frame1_windowed = apply_radial_window(frame1, radius_factor, decay_rate);
frame2_windowed = apply_radial_window(frame2, radius_factor, decay_rate);
% Adjust frame dimensions after applying buffer
frame1_windowed = frame1_windowed(y_buffer_range, x_buffer_range);
frame2_windowed = frame2_windowed(y_buffer_range, x_buffer_range);
% Perform wavelet transform on the entire frames
cwt1_full = cwtft2(frame1_windowed, 'wavelet', 'cauchy', 'scales', Scales, 'angles', Angles);
spec1_full = squeeze(cwt1_full.cfs);
cwt2_full = cwtft2(frame2_windowed, 'wavelet', 'cauchy', 'scales', Scales, 'angles', Angles);
spec2_full = squeeze(cwt2_full.cfs);
% Loop over squares
for s_idx = 1:length(squares)
square = squares(s_idx);
% Extract the square from both frames' wavelet coefficients
% Get the indices for the square in the cwt coefficient arrays
x_range = square.x_range - window_buffer;
y_range = square.y_range - window_buffer;
% Extract the cwt coefficients for the square
spec1 = spec1_full(y_range, x_range, :, :);
spec2 = spec2_full(y_range, x_range, :, :);
% Optionally save the square's metadata (CWT coefficients from spec1)
if Save_Metadata_Flag == 1
% Save the CWT coefficients for this square (only from spec1)
metadata_entry.frame_idx = frame_idx;
metadata_entry.square_idx = s_idx;
metadata_entry.spec1 = spec1; % Only saving spec1 (coefficients of the first frame)
square_metadata = [square_metadata; metadata_entry];
end
% Check brightness and standard deviation on the original frames
img1 = frame1_windowed(y_range, x_range);
mean_brightness = mean(img1(:));
std_brightness = std(img1(:));
if mean_brightness < brightness_threshold || std_brightness > std_threshold
% Ignore this square
continue;
end
% Proceed with the analysis using the extracted CWT coefficients
% Compute the cross-wavelet spectrum (XWT)
xwt = spec1 .* conj(spec2);
% Compute coherence and phase difference
power1 = abs(spec1).^2;
power2 = abs(spec2).^2;
coherence = abs(xwt);
phase_difference = angle(xwt);
% Find peaks in power spectrum and calculate speeds
peak_list = find_peaks_and_speeds(coherence, phase_difference, Scales, Angles, pixel_size_km, time_interval);
% Number of scales
n_scales = size(peak_list, 1);
% Prepare data to store
data_to_store = [repmat(frame_idx, n_scales, 1), repmat(s_idx, n_scales, 1), peak_list];
% Update entry counter
idx_start = entry_counter + 1;
idx_end = entry_counter + n_scales;
% Ensure we do not exceed preallocated array size
if idx_end > total_entries
% Expand the array if necessary
extra_entries = total_entries; % Double the size
peak_data = [peak_data; NaN(extra_entries, 8)]; % Now 8 columns to match new structure
total_entries = total_entries + extra_entries;
end
% Store data
peak_data(idx_start:idx_end, :) = data_to_store;
% Update the entry counter
entry_counter = idx_end;
end
% Display time taken to process this frame
fprintf('Time to process frame %d: %.2f seconds.\n', frame_idx, toc(frame_time));
end
% Remove unused (NaN) entries from peak_data
peak_data = peak_data(1:entry_counter, :);
% Display total execution time
fprintf('Total execution time: %.2f seconds.\n', toc(total_time));
%% Analyze the data
% Carrés d'intérêt
squares_of_interest = [9, 10, 13, 14];
target_wavelength = wavelength/1000; % Longueur d'onde cible en km
target_angle = direction; % Direction en degrés
% Filtrer les données pour les carrés d'intérêt
filtered_data = peak_data(ismember(peak_data(:, 2), squares_of_interest), :);
% Initialiser une liste pour stocker les vitesses proches de la longueur d'onde cible
speeds_near_target = [];
angles_near_target = [];
% Boucle sur chaque carré d'intérêt
for square = squares_of_interest
% Extraire les données pour le carré actuel
square_data = filtered_data(filtered_data(:, 2) == square, :);
% Calculer la différence en valeur absolue entre chaque longueur d'onde et la cible
[~, idx] = min(abs(square_data(:, 8) - target_wavelength));
% Récupérer la vitesse associée à la longueur d'onde la plus proche de 150 km
closest_speed = square_data(idx, 6); % La vitesse est dans la 6ème colonne
closest_angle = square_data(idx, 4); % L'angle est dans la 6ème colonne
% Ajouter cette vitesse à la liste des vitesses proches de la cible
speeds_near_target(end + 1) = closest_speed;
angles_near_target(end + 1) = closest_angle;
end
% Calculer la vitesse moyenne
average_speed = mean(speeds_near_target);
average_angle = 180*mean(angles_near_target)/pi;
% Afficher le résultat
fprintf('La vitesse moyenne pour les carrés %s proche de la longueur d''onde de %.2f km est : %.2f m/s\n', ...
num2str(squares_of_interest), target_wavelength,average_speed);
fprintf('L''angle moyen pour les carrés %s proche de la longueur d''onde de %.2f km est : %.2f °\n', ...
num2str(squares_of_interest), target_wavelength, average_angle);
% Calculer la différence en valeur absolue entre chaque longueur d'onde et la cible
[~, scale_idx] = min(abs(Scales_km*2 - target_wavelength));
target_angle_rad = mod((target_angle) * pi / 180, pi); % Ramener dans [0, pi[
[~, angle_idx] = min(abs(Angles - target_angle_rad));
%%
power_spectrum_2D = get_power_spectrum(cwt1_full, scale_idx, angle_idx, Scales_km, Angles,frame1_windowed,0.4);
% for i=1:length(Angles)
% power_spectrum_2D = get_power_spectrum(cwt1_full, scale_idx, i, Scales_km, Angles,frame1_windowed,0.8);
% end
%plot_waverose(1,91,false,4);
power_wave_rose(cwt1_full, Scales_km, Angles,length(Scales_km))
%% Supporting Functions
function img_processed = preprocess_img(img)
% Preprocess the image by truncating intensity values to reduce contrast.
% Calculate the 10th and 99th percentiles of the image intensity.
lower_bound = prctile(img(:), 10);
upper_bound = prctile(img(:), 99);
% Truncate values below the 10th percentile to the lower bound
% and values above the 99th percentile to the upper bound.
img_processed = img;
img_processed(img < lower_bound) = lower_bound;
img_processed(img > upper_bound) = upper_bound;
% Normalize to the [0, 1] range.
img_processed = (img_processed - lower_bound) / (upper_bound - lower_bound);
end
function peak_list = find_peaks_and_speeds(coherence, phase_difference, Scales, Angles, pixel_size_km, time_interval)
% Initialize peak_list to collect data for each scale
num_scales = length(Scales);
peak_list = zeros(num_scales, 6); % Columns: [Scale; Angle; MeanPhaseDiff; Speed_m_per_s; CoherenceValue; Wavelength_km]
% Loop over each scale
for scale_idx = 1:num_scales
% Extract coherence at the current scale across all angles
% Average coherence over x and y dimensions for each angle
coherence_scale = squeeze(mean(mean(coherence(:, :, scale_idx, :), 1, 'omitnan'), 2, 'omitnan')); % (num_angles x 1)
% Find the angle index with the maximum coherence value at this scale
[max_coherence_value, angle_idx] = max(coherence_scale);
% Get the corresponding angle
angle_target = Angles(angle_idx);
% Extract the phase difference and coherence slices at this scale and angle
phase_slice = phase_difference(:, :, scale_idx, angle_idx);
coherence_slice = coherence(:, :, scale_idx, angle_idx);
% Compute the mean phase difference over the square
% Optionally, apply a coherence threshold to mask out low coherence regions
coherence_threshold = 0.5 * max(coherence_slice(:));
coherence_mask = coherence_slice >= coherence_threshold;
if any(coherence_mask(:))
mean_phase_diff = mean(phase_slice(coherence_mask), 'omitnan');
else
mean_phase_diff = NaN; % No significant coherence regions
end
% Calculate speed
%wavelength_km = Scales(scale_idx) * 2 * pixel_size_km; % Wavelength in km
wavelength_km = (pi * Scales(scale_idx) * pixel_size_km) / sqrt(2);
distance_shift_km = (mean_phase_diff * wavelength_km) / (2 * pi);
speed_m_per_s = (distance_shift_km / time_interval) * 1000; % Convert km/s to m/s
% Adjust the speed based on the angle
%speed_m_per_s = speed_m_per_s * cos(angle_target);
% Store data
peak_list(scale_idx, :) = [Scales(scale_idx), angle_target, mean_phase_diff, speed_m_per_s, max_coherence_value, wavelength_km];
end
end
function img_windowed = apply_radial_window(img, radius_factor, decay_rate)
% Apply a radial windowing effect that attenuates the image from the edges towards the center
% Parameters:
% - radius_factor: controls how quickly the window decays from the center (e.g., 1.1 means 110% of the image radius)
% - decay_rate: controls the steepness of the attenuation (larger value makes the transition steeper)
[rows, cols] = size(img);
% Compute the center of the image
center_x = cols / 2;
center_y = rows / 2;
% Create a meshgrid to calculate the distance from the center
[x, y] = meshgrid(1:cols, 1:rows);
% Calculate the radial distance from the center for each pixel
distances = sqrt((x - center_x).^2 + (y - center_y).^2);
% Calculate the maximum distance from the center (i.e., the radius of the window)
max_distance = radius_factor * min(center_x, center_y); % Factor of the image size
% Create the radial window using a smooth logistic function
window = 1 ./ (1 + exp(decay_rate * (distances - max_distance)));
% Apply the window to the image
img_windowed = img .* window;
end
function plot_waverose(frame_id, square_id, display_sine_wave_plots, figs_per_page)
%% Plot Waverose Function with Enhanced Features
% This function generates an advanced rose plot for a specific frame and square.
% It also plots the coherence and phase difference maps for each peak.
%
% Parameters:
% - frame_id: ID of the frame to process
% - square_id: ID of the square within the frame
% - display_sine_wave_plots: (optional) true/false to display sine wave plots (default: true)
% - figs_per_page: (optional) number of subplots per figure page (default: 20)
% Set default values for optional parameters
if nargin < 3
display_sine_wave_plots = true;
end
if nargin < 4
figs_per_page = 20;
end
% Retrieve necessary variables from the base workspace
data_dir = evalin('base', 'data_dir');
nc_files = evalin('base', 'nc_files');
invshrinkfactor = evalin('base', 'invshrinkfactor');
pixel_size_km = evalin('base', 'pixel_size_km');
Scales = evalin('base', 'Scales');
Angles = evalin('base', 'Angles');
time_interval = evalin('base', 'time_interval');
squares = evalin('base', 'squares');
Preprocess_Flag = evalin('base', 'Preprocess_Flag');
window_buffer = evalin('base', 'window_buffer');
radius_factor = evalin('base', 'radius_factor');
decay_rate = evalin('base', 'decay_rate');
shrinkfactor = evalin('base', 'shrinkfactor');
brightness_threshold = evalin('base', 'brightness_threshold');
std_threshold = evalin('base', 'std_threshold');
%% Read Frames
% Check if frame_id is valid
if frame_id < 1 || frame_id >= length(nc_files)
error('Invalid frame_id. Must be between 1 and %d.', length(nc_files) - 1);
end
% Read the specified frames
file1_path = fullfile(data_dir, nc_files(frame_id).name);
file2_path = fullfile(data_dir, nc_files(frame_id + 1).name);
frame1 = readVariableFromFile(file1_path);
frame2 = readVariableFromFile(file2_path);
% Transpose the frames to switch x and y dimensions (portrait orientation)
frame1 = frame1';
frame2 = frame2';
% Resize frames according to shrink factor if needed
if shrinkfactor ~= 1
frame1 = imresize(frame1, invshrinkfactor);
frame2 = imresize(frame2, invshrinkfactor);
end
% Preprocess frames if needed
if Preprocess_Flag
frame1 = preprocess_img(frame1);
frame2 = preprocess_img(frame2);
end
% Apply windowing to the entire frame
frame1_windowed = apply_radial_window(frame1, radius_factor, decay_rate);
frame2_windowed = apply_radial_window(frame2, radius_factor, decay_rate);
% Adjust frame dimensions after applying buffer
[frame_height, frame_width] = size(frame1);
x_buffer_range = (window_buffer + 1):(frame_width - window_buffer);
y_buffer_range = (window_buffer + 1):(frame_height - window_buffer);
frame1_windowed = frame1_windowed(y_buffer_range, x_buffer_range);
frame2_windowed = frame2_windowed(y_buffer_range, x_buffer_range);
%% Perform Wavelet Transform on Full Frames
cwt1_full = cwtft2(frame1_windowed, 'wavelet', 'cauchy', 'scales', Scales, 'angles', Angles);
spec1_full = squeeze(cwt1_full.cfs);
cwt2_full = cwtft2(frame2_windowed, 'wavelet', 'cauchy', 'scales', Scales, 'angles', Angles);
spec2_full = squeeze(cwt2_full.cfs);
%% Extract the Specified Square
% Find the square with the given square_id
idx = find([squares.index] == square_id);
if isempty(idx)
error('Invalid square_id. Square not found.');
end
square = squares(idx);
% Extract the square from both frames' wavelet coefficients
x_range = square.x_range - window_buffer;
y_range = square.y_range - window_buffer;
% Ensure the ranges are within the valid indices
x_range(x_range < 1) = [];
y_range(y_range < 1) = [];
x_range(x_range > size(spec1_full, 2)) = [];
y_range(y_range > size(spec1_full, 1)) = [];
% Extract the cwt coefficients for the square
spec1 = spec1_full(y_range, x_range, :, :);
spec2 = spec2_full(y_range, x_range, :, :);
% Check brightness and standard deviation on the original frames
img1 = frame1_windowed(y_range, x_range);
mean_brightness = mean(img1(:));
std_brightness = std(img1(:));
if mean_brightness < brightness_threshold || std_brightness > std_threshold
error('The selected square does not meet brightness or standard deviation thresholds.');
end
%% Compute Cross-Wavelet Spectrum, Coherence, Phase Difference
% Compute the cross-wavelet spectrum (XWT)
xwt = spec1 .* conj(spec2);
% Compute coherence and phase difference
power1 = abs(spec1).^2;
power2 = abs(spec2).^2;
coherence = abs(xwt);
phase_difference = angle(xwt);
%% Find Peaks and Speeds (One Peak per Scale)
peak_list = find_peaks_and_speeds(coherence, phase_difference, Scales, Angles, pixel_size_km, time_interval);
%% Plot Advanced Rose Plot
% Generate the advanced rose plot with power and coherence
% Overlay the peaks on the plot
% Calculate inner power and coherence for plotting
buffer = 0; % Adjust buffer as needed
innerpower = squeeze(mean(mean(power1(buffer+1:end-buffer, buffer+1:end-buffer, :, :), 1, 'omitnan'), 2, 'omitnan')); % (num_scales x num_angles)
innercoherence = squeeze(mean(mean(coherence(buffer+1:end-buffer, buffer+1:end-buffer, :, :), 1, 'omitnan'), 2, 'omitnan')); % (num_scales x num_angles)
% Normalize power and coherence by scale
mean_power_byscale = mean(innerpower, 2);
mean_coherence_byscale = mean(innercoherence, 2);
anglespec_power = innerpower ./ mean_power_byscale;
anglespec_coherence = innercoherence ./ mean_coherence_byscale;
% Define angles for upper and lower halves
Angles_pos = Angles;
Angles_neg = Angles + pi;
% Prepare data for plotting
[Theta_pos, R_pos] = meshgrid(Angles_pos, Scales);
[X_pos, Y_pos] = pol2cart(Theta_pos, R_pos);
[Theta_neg, R_neg] = meshgrid(Angles_neg, Scales);
[X_neg, Y_neg] = pol2cart(Theta_neg, R_neg);
% Plot the advanced rose plot
figure;
% Plot power (upper half)
ax1 = axes;
pcolor(ax1, X_pos, Y_pos, anglespec_power);
shading interp;
colormap(ax1, 'parula');
axis equal;
set(ax1, 'Position', [0.1, 0.1, 0.75, 0.75]);
ax1.XTick = [];
ax1.YTick = [];
hold on;
% Plot coherence (lower half)
ax2 = axes;
pcolor(ax2, X_neg, Y_neg, anglespec_coherence);
shading interp;
colormap(ax2, 'autumn');
axis equal;
set(ax2, 'Position', [0.1, 0.1, 0.75, 0.75]);
ax2.XTick = [];
ax2.YTick = [];
set(ax2, 'Color', 'none');
linkaxes([ax1, ax2]);
hold on;
% Overlay peaks on the coherence plot
if ~isempty(peak_list)
% Since peak_list is now (num_scales x 8)
max_scales = peak_list(:,1); % Scales
max_angles = peak_list(:,2) + pi; % Adjust angles for lower half
[peak_X, peak_Y] = pol2cart(max_angles, max_scales);
plot(ax2, peak_X, peak_Y, 'k*', 'MarkerSize', 10);
% Annotate peaks with speeds and wavelengths
for i = 1:length(peak_X)
% Wavelength in km
wavelength_km = peak_list(i,6);
% Display wavelength in km
text(ax2, peak_X(i) * 1.05, peak_Y(i) * 1.05, sprintf('%.1f km', wavelength_km), 'Color', 'k', 'FontSize', 10);
end
end
% Adjust axes limits
xlim(ax1, [min(X_pos(:)) - 1, max(X_pos(:)) + 1]);
ylim(ax1, [min(Y_neg(:)) - 1, max(Y_pos(:)) + 1]);
%% Add Radial Rings and Angle Labels
% Add radial rings corresponding to scales (in km)
wavelengths_km = Scales * pi/sqrt(2) * pixel_size_km; % Convert scales to wavelengths in km
ring_radii = Scales;
for i = 1:length(ring_radii)
theta_ring = linspace(0, 2 * pi, 100);
[x_ring, y_ring] = pol2cart(theta_ring, ring_radii(i));
plot(ax1, x_ring, y_ring, 'k--');
plot(ax2, x_ring, y_ring, 'k--');
% Add scale labels in km
text(ax1, ring_radii(i) * 1.05, 0, sprintf('%.1f km', wavelengths_km(i)), 'HorizontalAlignment', 'left');
end
% Add angle lines and labels
angle_ticks = linspace(0, 2 * pi, 13); % Every 30 degrees
angle_labels = {'0', '\pi/6', '\pi/3', '\pi/2', '2\pi/3', '5\pi/6', '\pi', '7\pi/6', '4\pi/3', '3\pi/2', '5\pi/3', '11\pi/6', '2\pi'};
for i = 1:length(angle_ticks)
angle_rad = angle_ticks(i);
x_line = [0, max(Scales) * cos(angle_rad)];
y_line = [0, max(Scales) * sin(angle_rad)];
plot(ax1, x_line, y_line, 'k--');
plot(ax2, x_line, y_line, 'k--');
text(ax1, x_line(2) * 1.1, y_line(2) * 1.1, angle_labels{i}, 'HorizontalAlignment', 'center');
end
%% Add Colorbars
original_pos = get(ax1, 'Position');
c1 = colorbar(ax1, 'eastoutside');
c1_pos = get(c1, 'Position');
c1_pos(1) = c1_pos(1) + 0.05;
set(c1, 'Position', c1_pos);
set(ax1, 'Position', original_pos);
ylabel(c1, 'Power');
original_pos = get(ax2, 'Position');
c2 = colorbar(ax2, 'westoutside');
c2_pos = get(c2, 'Position');
c2_pos(1) = c2_pos(1) - 0.05;
set(c2, 'Position', c2_pos);
set(ax2, 'Position', original_pos);
ylabel(c2, 'Coherence');
%% Set Titles
sgtitle(sprintf('Advanced Rose Plot for Frame %d and Square %d', frame_id, square_id));
%% Additional Plots: Coherence Maps and Phase Difference Maps
% Define common fractions of pi for displaying angles
pi_fractions = {'0', '\pi/6', '\pi/4', '\pi/3', '\pi/2', '2\pi/3', '\pi', '4\pi/3', '3\pi/2', '2\pi'};
pi_fraction_values = [0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, pi, 4*pi/3, 3*pi/2, 2*pi];
% Preallocate a cell array to store coherence masks
coherence_masks = cell(size(peak_list,1),1);
% Plot coherence with mask for each scale
num_peaks = size(peak_list, 1);
% Calculate the number of figures needed based on figs_per_page
num_figures = ceil(num_peaks / figs_per_page);
current_peak = 1;
for fig_num = 1:num_figures
figure;
num_subplots = min(figs_per_page, num_peaks - (fig_num - 1) * figs_per_page);
ncols = ceil(sqrt(num_subplots));
nrows = ceil(num_subplots / ncols);
for subplot_idx = 1:num_subplots
i = current_peak;
% Extract the real scale and angle from the peak_list
real_scale = peak_list(i,1); % Scale from peak_list
real_angle = peak_list(i,2); % Angle from peak_list
% Find the index of the closest scale in the Scales array
[~, scale_idx] = min(abs(Scales - real_scale));
% Adjust real_angle if necessary (wrap around)
real_angle = mod(real_angle, 2*pi);
% Find the index of the closest angle in the Angles array
[~, angle_idx] = min(abs(Angles - real_angle));
% Extract the corresponding coherence slice for the current scale and angle
coherence_slice = coherence(:, :, scale_idx, angle_idx);
% Define the threshold for the top 60% of the max coherence value
coherence_max = max(coherence_slice(:));
coherence_threshold = 0.6 * coherence_max;
% Create the mask where coherence is above the threshold
coherence_mask = coherence_slice >= coherence_threshold;
% Store the mask in the cell array
coherence_masks{i} = coherence_mask; % Store mask for later use
% Plot the coherence using imagesc
subplot(nrows, ncols, subplot_idx); % Subplot for multiple plots
imagesc(coherence_slice);
hold on;
% Overlay the contour of the coherence mask
contour(coherence_mask, [1 1], 'r', 'LineWidth', 1); % Red contour at mask boundary
% Ensure axis is tight
axis tight;
% Convert the real_angle to a fraction of pi for the title
[~, angle_fraction_idx] = min(abs(pi_fraction_values - real_angle)); % Find closest pi fraction
angle_str = pi_fractions{angle_fraction_idx}; % Get the corresponding fraction of pi string
% Set title with scale and angle in fractions of pi
wavelength_km = real_scale * pi * pixel_size_km/sqrt(2);
title(sprintf('Wavelength: %.1f km, Angle: %s', wavelength_km, angle_str));
% Customize the colorbar
colorbar;
% Set axis equal for consistent plotting
axis equal;
hold off;
current_peak = current_peak + 1;
end
sgtitle('Coherence with Mask for Each Scale/Angle Peak');
end
%% Plot Phase Difference with Masked Values and Compute Mean
current_peak = 1;
for fig_num = 1:num_figures
figure;
num_subplots = min(figs_per_page, num_peaks - (fig_num - 1) * figs_per_page);
ncols = ceil(sqrt(num_subplots));
nrows = ceil(num_subplots / ncols);
for subplot_idx = 1:num_subplots
i = current_peak;
% Extract the real scale and angle from the peak_list
real_scale = peak_list(i,1); % Scale from peak_list
real_angle = peak_list(i,2); % Angle from peak_list
% Find the index of the closest scale in the Scales array
[~, scale_idx] = min(abs(Scales - real_scale));
% Adjust real_angle if necessary (wrap around)
real_angle = mod(real_angle, 2*pi);
% Find the index of the closest angle in the Angles array
[~, angle_idx] = min(abs(Angles - real_angle));
% Convert the real_angle to a fraction of pi for the title
[~, angle_fraction_idx] = min(abs(pi_fraction_values - real_angle)); % Find closest pi fraction
angle_str = pi_fractions{angle_fraction_idx}; % Get the corresponding fraction of pi string
% Extract the corresponding phase_difference slice for the current scale and angle
phase_slice = phase_difference(:, :, scale_idx, angle_idx);
% Retrieve the corresponding mask from coherence_masks
coherence_mask = coherence_masks{i};
% Apply the mask from coherence to the phase slice
phase_masked = phase_slice;
phase_masked(~coherence_mask) = NaN; % Set non-coherent areas to NaN
% Plot the phase_difference using imagesc
subplot(nrows, ncols, subplot_idx); % Subplot for multiple plots
imagesc(phase_masked);
hold on;
% Overlay the contour of the coherence mask
contour(coherence_mask, [1 1], 'r', 'LineWidth', 1); % Red contour at mask boundary
% Set title with scale and angle in fractions of pi
wavelength_km = real_scale * pi * pixel_size_km/sqrt(2);
title(sprintf('Wavelength: %.1f km, Angle: %s', wavelength_km, angle_str));
% Customize the colorbar to display ticks from -pi to pi
c = colorbar;
caxis([-pi pi]); % Set color axis limits from -pi to pi
set(c, 'Ticks', [-pi, -pi/2, 0, pi/2, pi], 'TickLabels', {'-\pi', '-\pi/2', '0', '\pi/2', '\pi'});
% Set axis equal for consistent plotting
axis equal;
axis tight;
hold off;
current_peak = current_peak + 1;
end
sgtitle('Phase Difference with Masked Regions for Each Scale/Angle Peak');
end
%% Additional Plot: Overlay Wavelet Contours on the Image Zoomed into the Square
current_peak = 1;
for fig_num = 1:num_figures
figure;
num_subplots = min(figs_per_page, num_peaks - (fig_num - 1) * figs_per_page);
ncols = ceil(sqrt(num_subplots));
nrows = ceil(num_subplots / ncols);
for subplot_idx = 1:num_subplots
i = current_peak;
% Extract the real scale and angle from the peak_list
real_scale = peak_list(i,1); % Scale from peak_list
real_angle = peak_list(i,2); % Angle from peak_list
% Find the index of the closest scale in the Scales array
[~, scale_idx] = min(abs(Scales - real_scale));
% Adjust real_angle if necessary (wrap around)
real_angle = mod(real_angle, 2*pi);
% Find the index of the closest angle in the Angles array
[~, angle_idx] = min(abs(Angles - real_angle));
% Extract the corresponding frame square (use the windowed frame)
frame_square = frame1_windowed(y_range, x_range);
% Extract the wavelet coefficients for the square
spec_square = spec1(:, :, :, :);
% Plot using image_with_wavelet_overlay
subplot(nrows, ncols, subplot_idx);
clevfactor = 1; % Adjust as needed
ProcessFlag = 1; % Use ProcessFlag = 1 for normalized display
% Call the overlay function
image_with_wavelet_overlay(frame_square, spec_square, Scales, scale_idx, angle_idx, clevfactor, ProcessFlag);
% Overlay the mask contour from coherence_mask
coherence_mask = coherence_masks{i}; % Get the mask for this peak
hold on;
contour(coherence_mask, [1 1], 'magenta', 'LineWidth', 1); % Magenta contour at mask boundary
hold off;
% Set title with scale and angle
wavelength_km = real_scale * pi * pixel_size_km/sqrt(2);
title(sprintf('Wavelength: %.1f km, Angle: %.2f°', wavelength_km, real_angle * (180/pi)));
% Ensure axis is equal and tight
axis equal;
axis tight;
current_peak = current_peak + 1;
end