[Scilab-users] Padding and clipping a grid

classic Classic list List threaded Threaded
15 messages Options
arctica1963 arctica1963
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] Padding and clipping a grid

Hello all,

I need to increase the dimensions over which the FFT is calculated to avoid
edge effects, and following the suggestion of Samuel Gougeon back in 2016,
this was recommended:

Scilab: 6.0.1

data = [1 2 3; 4 5 6; 7 8 9]
addedRows = 3; addedCols = 3;

tmp = [data ; data($, :) .*. ones(addedRows, 1)]; // add rows to bottom
tmp = [tmp  tmp(:, $) .*. ones(1,addedCols)]; // add columns to right
tmp = [repmat(tmp(1, : ), addedRows, 1) ; tmp]; // add rows to top
paddedMat = [repmat(tmp(:,1) , 1, addedCols) tmp] // add columns to left

Now, it is important to adjust the input spatial limits (e.g. degrees) for
the new grid:

e.g. x1 = -1, x2 = 1, y1 = -1, y2 = 1 original spatial limits

pad = 1 // add 1 unit to all edges
x1a = x1 - pad
x2a = x2 + pad
y1a = y1 - pad
y2a = y2 + pad

xx1a = linspace(x1a,x2a,new_xdim)
yy1a = linspace(y1a,y2a,new_ydim)

[X,Y] = meshgrid(xx1a,yy1a) etc.

What I would like to do is then run the FFT on the expanded grid and then
clip it back to the original dimensions (before padding) and spatial limits
after processing.

Is there a simple solution to this? I was thinking
resize_matrix(data,dimx,dimy), but not sure how to get the original grid
clipped out. So in the example above, go from the padded limits (-2,2,-2,2)
back to (-1,1,-1,1).

Sorry if this is a really simple solution, still learning!

Thanks
Lester



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Hello Lester,

Le 29/07/2018 à 13:18, arctica1963 a écrit :
> Hello all,
>
> I need to increase the dimensions over which the FFT is calculated to avoid
> edge effects,

AFAIK, padding the initial signal does not avoid edge effects (i guess
one deal with edges discontinuity).
It can even be the source of discontinuity, and so yield fake high fft
amplitudes at high frequencies.
It will be the case if  the original signal S has no edge discontinuity
( S(1)~S($)) but you padd
it with zeros while its edge does not fall to zeros (=> |S($)|>>0,
S($+1)==0). Even worse,
in this case, windowing the signal will no longer be effective, since
the padding will move
the discontinuity in the body of the padded signal instead of on
signal's edge.

Padding a signal increases the lowest frequency of its numerical FT,
that is the sampling frequency
of its FFT. AFAIUnderstand, there is no edge matter there.

But this is more a signal processing topic than a Scilab one.

Regards
Samuel

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

In reply to this post by arctica1963
Le 29/07/2018 à 13:56, Samuel Gougeon a écrit :
Hello Lester,

Le 29/07/2018 à 13:18, arctica1963 a écrit :
Hello all,

I need to increase the dimensions over which the FFT is calculated to avoid
edge effects,

AFAIK, padding the initial signal does not avoid edge effects (i guess one deal with edges discontinuity).
It can even be the source of discontinuity, and so yield fake high fft amplitudes at high frequencies.
It will be the case if  the original signal S has no edge discontinuity ( S(1)~S($)) but you padd
it with zeros while its edge does not fall to zeros (=> |S($)|>>0, S($+1)==0). Even worse,
in this case, windowing the signal will no longer be effective, since the padding will move
the discontinuity in the body of the padded signal instead of on signal's edge.

Padding a signal increases the lowest frequency of its numerical FT, that is the sampling frequency
of its FFT. AFAIUnderstand, there is no edge matter there.

But this is more a signal processing topic than a Scilab one.

Le 29/07/2018 à 13:18, arctica1963 a écrit :
Hello all,

I need to increase the dimensions over which the FFT is calculated to avoid
edge effects, and following the suggestion of Samuel Gougeon back in 2016,
this was recommended:

Scilab: 6.0.1

data = [1 2 3; 4 5 6; 7 8 9]
addedRows = 3; addedCols = 3;

tmp = [data ; data($, :) .*. ones(addedRows, 1)]; // add rows to bottom
tmp = [tmp  tmp(:, $) .*. ones(1,addedCols)]; // add columns to right
tmp = [repmat(tmp(1, : ), addedRows, 1) ; tmp]; // add rows to top
paddedMat = [repmat(tmp(:,1) , 1, addedCols) tmp] // add columns to left

--> paddedMat = [repmat(tmp(:,1) , 1, addedCols) tmp] // add columns to left
 paddedMat  =

   1.   1.   1.   1.   2.   3.   3.   3.   3.
   1.   1.   1.   1.   2.   3.   3.   3.   3.
   1.   1.   1.   1.   2.   3.   3.   3.   3.
   1.   1.   1.   1.   2.   3.   3.   3.   3.
   4.   4.   4.   4.   5.   6.   6.   6.   6.
   7.   7.   7.   7.   8.   9.   9.   9.   9.
   7.   7.   7.   7.   8.   9.   9.   9.   9.
   7.   7.   7.   7.   8.   9.   9.   9.   9.
   7.   7.   7.   7.   8.   9.   9.   9.   9.


OK: here the padding is not done with zeros. Sorry for my misunderstanding.


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

In reply to this post by Samuel GOUGEON

Hi,

 

A standard practice is to apply tapering then padding (with zeros).

 

Regards,

Rafael

 

 


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Le 29/07/2018 à 14:07, Rafael Guerra a écrit :

Hi,

 

A standard practice is to apply tapering then padding (with zeros).


AKA windowing. In Scilab: https://help.scilab.org/docs/6.0.1/en_US/window.html
2D windows can be built from these 1D profiles.


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Le 29/07/2018 à 14:25, Samuel Gougeon a écrit :
Le 29/07/2018 à 14:07, Rafael Guerra a écrit :

Hi,

 

A standard practice is to apply tapering then padding (with zeros).


AKA windowing. In Scilab: https://help.scilab.org/docs/6.0.1/en_US/window.html
2D windows can be built from these 1D profiles.

By the way, it could be useful to upgrade window() in order to become able to generate
such 2D or N-D windows without external postprocessing.


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
arctica1963 arctica1963
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

In reply to this post by Samuel GOUGEON
Hi all,

I will have to look further into this. I can check the results against GMT
(grdfft), I think they may use tapering but will have to look at the code
(C++).

Can I confirm that clipping is done via resize_matrix?

Cheers
Lester





--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Le 29/07/2018 à 14:53, arctica1963 a écrit :
> Hi all,
>
> I will have to look further into this. I can check the results against GMT
> (grdfft), I think they may use tapering but will have to look at the code
> (C++).
>
> Can I confirm that clipping is done via resize_matrix?
Not if it is symetric.

The f = fft() result put high frequencies (to be removed) at the center
of the spectrum.
So, if you don't use fftshift() to put it on the spectrum's edges, you
will have to do someting like:

pw = [pwr, pwc];  // predefined padding widths
s = size(f);
tf = f([1:s(1)/2-pwr s(1)/2+pwr:s(1)], [1:s(2)/2-pwc s(2)/2+pwc:s(2)];

But this expression is approximative, depending on the oddness of s
components, etc.
It is easier to trimmed the shifted spectrum:

sf = fftshift(f);
tsf = sf(pwr+1:$-pwr, pwc+1:$-pwc);

and then backshift the trimmed spectrum:
sf = fftshift(tsf);

This is the idea.
Samuel

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Le 29/07/2018 à 15:06, Samuel Gougeon a écrit :

But this expression is approximative, depending on the oddness of s components, etc.
Sorry for my bad (and too fast) english. I meant: the parity: if each size is even or odd.


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

In reply to this post by Samuel GOUGEON
Le 29/07/2018 à 14:48, Samuel Gougeon a écrit :
Le 29/07/2018 à 14:25, Samuel Gougeon a écrit :
Le 29/07/2018 à 14:07, Rafael Guerra a écrit :

Hi,

 

A standard practice is to apply tapering then padding (with zeros).


AKA windowing. In Scilab: https://help.scilab.org/docs/6.0.1/en_US/window.html
2D windows can be built from these 1D profiles.

By the way, it could be useful to upgrade window() in order to become able to generate
such 2D or N-D windows without external postprocessing.

Wish now reported as bug 15694.

See also https://en.wikipedia.org/wiki/Window_function#Two-dimensional_windows


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
arctica1963 arctica1963
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Thanks for the suggestions and clarification.





--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

In reply to this post by Samuel GOUGEON

Hi,

 

A more detailed reference is:

https://en.wikipedia.org/wiki/Two_dimensional_window_design

 

In the simplest approach, the 2D window is the outer product of two 1D windows:  Wxy = wx'*wy

 

For example:

   nx=100;

   ny=200;

   wx = window('hm',nx);

   wy = window('hm',ny);

   ix = linspace(0,1,length(wx));

   iy = linspace(0,1,length(wy));

   Wxy = wx'*wy;

   clf()

   gcf().color_map = hotcolormap(64);

   Sgrayplot(ix,iy,Wxy)

 

Regards,

Rafael


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
arctica1963 arctica1963
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Hi Rafael,

I reviewed the methodology applied in GMT (grdfft) and they use tapering
from the grid edges to zero. The documentation page details more:

GMT_grdfft <http://gmt.soest.hawaii.edu/doc/5.4.4/grdfft.html>  

The padding method that has been discussed previously is the similar to that
of grdfft edge-point symmetry, but without the tapering to zero.

It would be handy to have this padding and tapering better defined in Scilab
for 2D grids.

Lester



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Hi Lester,

Thanks for the pointer to the GMT (grdfft) documentation.
An additional standard pre-processing step is detrending (removing the best-fitting linear trend).

Regards,
Rafael


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
arctica1963 arctica1963
Reply | Threaded
Open this post in threaded view
|

Re: Padding and clipping a grid

Hi Rafael,

Forgot to say, the source code is available for all of the GMT modules, as
log as you understand C++

I just found reference to detrending prior to fft, thanks for confirming
that process!

Lester



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users