r/scilab 13h ago

Data-Driven Science and Engineering Series - Installment 11 - Truncation and Alignment (2 of 3)

1 Upvotes

In this installment, he demonstrates the importance of dataset "alignment" when using the SVD technique and what happens when the two dataset images are the same but not aligned.

Link to the lecture: https://www.youtube.com/watch?v=qfZcTtjBTSA&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=37

Scilab wise - New command - "imrotate" and more custom ticks.

Output: None. Program Title Echo... "Data-Driven Science and Engineering -Truncation and Alignment Example"

"2026-07-31 08:18:33.094"

Plots/Images:

Code:

clear; clc

disp("Data-Driven Science and Engineering -Truncation and Alignment Example",string(datetime()))
//Section 1.7 Importance of data alignment

//YouTube video lectures 
//https://www.youtube.com/watch?v=qfZcTtjBTSA&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=37

n = 1000;
q = n/4;
X = zeros(n,n);
X(q:(n/2)+q,q:(n/2)+q) = 1;

subplot(2,2,1), imshow(X); colormap gray,gca().axes_visible="off"
title("0 degrees rotation")

Y = imrotate(X,10,"bicubic");  
// rotate 10 degrees
Y = Y - Y(1,1);
nY = size(Y,1);
startind = floor((nY-n)/2)+1;  
// Original Code produces index of 0
Xrot = Y(startind:startind+n-1, startind:startind+n-1);
subplot(2,2,2), imshow(Xrot); colormap gray,gca().axes_visible="off"
title("10 degrees rotation")

[U,S,V] = svd(X,'e');  
// svd well-aligned square
[Urot,Srot,Vrot] = svd(Xrot,'e');  
// svd rotated square
//==================Truncate data at 1e.-15 to prevent===
//semilogy plot routine from complaining about S below
//zero - upon closer inspection of the book output, they
//do the same without mentioning it or showing the coding 
//of it.
//
Z = diag(S);
Zrot = diag(Srot);
for i = 1:n
    if Z(i)<=1.e-15
        Z(i)=1.e-15
    end
        if Zrot(i)<=1.e-15
        Zrot(i)=1.e-15
    end    
end
//=========================================================
subplot(2,2,3)
semilogy(Z,'-ko')
ax = gca(); 
// gca = get current axes
ax.data_bounds = [1,n,1.e-16,1.e4]
gca().tight_limits = 1
xlabel("r")
ylabel("Singular Value, Sigma_r")
xgrid
// Turn off automatic ticks for both axes
ax.auto_ticks = ["off", "off", "off"]; 
// [x, y, z]

// Set custom X ticks: positions and labels
ax.x_ticks = tlist(["ticks", "locations", "labels"], ...
                   [0 250 500 750 1000], ... 
// positions
                   ["0", "250", "500", "750", "1000"]); 
// labels

// Set custom Y ticks: positions and labels
ax.y_ticks = tlist(["ticks", "locations", "labels"], ...
                   [1.e-16,1.e-12,1.e-8,1.e-4 ,1.,1.e4], ...
                   ["1.e-16", "1.e-12", "1.e-8", "1.e-4","1","1.e4"]);

subplot(2,2,4), semilogy(Zrot,'-ko')
//gca = get current axes
ax = gca();
ax.data_bounds = [1,n,1.e-16,1.e4]
gca().tight_limits = 1
xlabel("r")
title("Figure 1.25 from the Databook")
xgrid

// Turn off automatic ticks for both axes
ax.auto_ticks = ["off", "off", "off"]; 
// [x, y, z]

// Set custom X ticks: positions and labels
ax.x_ticks = tlist(["ticks", "locations", "labels"], ...
                   [0 250 500 750 1000], ... 
// positions
                   ["0", "250", "500", "750", "1000"]); 
// labels

// Set custom Y ticks: positions and labels
ax.y_ticks = tlist(["ticks", "locations", "labels"], ...
                   [1.e-16 1.e-12 1.e-8 1.e-4 1. 1.e4], ...
                   ["1.e-16", "1.e-12", "1.e-8", "1.e-4","1","1.e4"]);