<< Chapter < Page | Chapter >> Page > |
The shiftOrigin method is shown in its entirety in Listing 4 . Although this method is rather long, it is also completely straightforward. Therefore, itshouldn't require a further explanation. You may be able to develop a much shorter algorithm for accomplishing the same task.
Listing 4. The shiftOrigin method code. |
---|
//Method to shift the wavenumber origin and
// place it at the center for a more visually// pleasing display. Must be applied
// separately to the real part, the imaginary// part, and the amplitude spectrum for a
// wavenumber spectrum.static double[][]shiftOrigin(double[][]data){
int numberOfRows = data.length;int numberOfCols = data[0].length;int newRows;
int newCols;double[][] output =new double[numberOfRows][numberOfCols];//Must treat the data differently when the
// dimension is odd than when it is even.if(numberOfRows%2 != 0){//oddnewRows = numberOfRows +
(numberOfRows + 1)/2;}else{//even
newRows = numberOfRows + numberOfRows/2;}//end elseif(numberOfCols%2 != 0){//odd
newCols = numberOfCols +(numberOfCols + 1)/2;
}else{//evennewCols = numberOfCols + numberOfCols/2;
}//end else//Create a temporary working array.double[][]temp =
new double[newRows][newCols];//Copy input data into the working array.for(int row = 0;row<numberOfRows;row++){
for(int col = 0;col<numberOfCols;col++){
temp[row][col] = data[row][col];}//col loop
}//row loop//Do the horizontal shift firstif(numberOfCols%2 != 0){//shift for odd
//Slide leftmost (numberOfCols+1)/2 columns// to the right by numberOfCols columns
for(int row = 0;row<numberOfRows;row++){
for(int col = 0;col<(numberOfCols+1)/2;col++){
temp[row][col + numberOfCols] =temp[row][col];
}//col loop}//row loop
//Now slide everything back to the left by// (numberOfCols+1)/2 columns
for(int row = 0;row<numberOfRows;row++){
for(int col = 0;col<numberOfCols;col++){
temp[row][col] =temp[row][col+(numberOfCols + 1)/2];
}//col loop}//row loop}else{//shift for even
//Slide leftmost (numberOfCols/2) columns// to the right by numberOfCols columns.
for(int row = 0;row<numberOfRows;row++){
for(int col = 0;col<numberOfCols/2;col++){
temp[row][col + numberOfCols] =temp[row][col];
}//col loop}//row loop//Now slide everything back to the left by
// numberOfCols/2 columnsfor(int row = 0;row<numberOfRows;row++){
for(int col = 0;col<numberOfCols;col++){
temp[row][col] =temp[row][col + numberOfCols/2];
}//col loop}//row loop
}//end else//Now do the vertical shift
if(numberOfRows%2 != 0){//shift for odd//Slide topmost (numberOfRows+1)/2 rows
// down by numberOfRows rows.for(int col = 0;col<numberOfCols;col++){
for(int row = 0;row<(numberOfRows+1)/2;row++){
temp[row + numberOfRows][col] =temp[row][col];
}//row loop}//col loop
//Now slide everything back up by// (numberOfRows+1)/2 rows.
for(int col = 0;col<numberOfCols;col++){
for(int row = 0;row<numberOfRows;row++){
temp[row][col] =temp[row+(numberOfRows + 1)/2][col];
}//row loop}//col loop}else{//shift for even
//Slide topmost (numberOfRows/2) rows down// by numberOfRows rows
for(int col = 0;col<numberOfCols;col++){
for(int row = 0;row<numberOfRows/2;row++){
temp[row + numberOfRows][col] =temp[row][col];
}//row loop}//col loop//Now slide everything back up by
// numberOfRows/2 rows.for(int col = 0;col<numberOfCols;col++){
for(int row = 0;row<numberOfRows;row++){
temp[row][col] =temp[row + numberOfRows/2][col];
}//row loop}//col loop
}//end else//Shifting of the origin is complete. Copy// the rearranged data from temp to output
// array.for(int row = 0;row<numberOfRows;row++){
for(int col = 0;col<numberOfCols;col++){
output[row][col] = temp[row][col];}//col loop
}//row loopreturn output;
}//end shiftOrigin method}//end class ImgMod30 |
Notification Switch
Would you like to follow the 'Digital signal processing - dsp' conversation and receive update notifications?