All Downloads are FREE. Search and download functionalities are using the official Maven repository.

example.SignalProcessing Maven / Gradle / Ivy

/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (c) 2016-2021 Barry DeZonia All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution.
 * 
 * Neither the name of the  nor the names of its contributors may
 * be used to endorse or promote products derived from this software without specific
 * prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package example;

import nom.bdezonia.zorbage.algebra.G;
import nom.bdezonia.zorbage.algorithm.ConvolveND;
import nom.bdezonia.zorbage.algorithm.CorrelateND;
import nom.bdezonia.zorbage.algorithm.FFT;
import nom.bdezonia.zorbage.algorithm.Fill;
import nom.bdezonia.zorbage.algorithm.InvFFT;
import nom.bdezonia.zorbage.algorithm.ResampleAveragedCubics;
import nom.bdezonia.zorbage.algorithm.ResampleAveragedLinears;
import nom.bdezonia.zorbage.algorithm.ResampleNearestNeighbor;
import nom.bdezonia.zorbage.data.DimensionedDataSource;
import nom.bdezonia.zorbage.data.DimensionedStorage;
import nom.bdezonia.zorbage.data.ProcedurePaddedDimensionedDataSource;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;
import nom.bdezonia.zorbage.oob.nd.ZeroNdOOB;
import nom.bdezonia.zorbage.sampling.IntegerIndex;
import nom.bdezonia.zorbage.type.complex.float64.ComplexFloat64Member;
import nom.bdezonia.zorbage.type.real.float16.Float16Member;
import nom.bdezonia.zorbage.type.real.highprec.HighPrecisionMember;

/**
 * @author Barry DeZonia
 */
class SignalProcessing {
	
	// Zorbage provides some signal processing algorithms
	
	// FFT / inverse FFT
	
	void example1() {
		
		IndexedDataSource orig =
				nom.bdezonia.zorbage.storage.Storage.allocate(G.CDBL.construct(), 512L);
	
		IndexedDataSource tmp =
				nom.bdezonia.zorbage.storage.Storage.allocate(G.CDBL.construct(), 512L);
		
		Fill.compute(G.CDBL, G.CDBL.random(), orig);
		
		FFT.compute(G.CDBL, G.DBL, orig, tmp);
		
		// < do something here: like make high modulus values into zeroes >
		
		InvFFT.compute(G.CDBL, G.DBL, tmp, orig);
		
		// here the original signal has now had high frequency values removed
	}
	
	// correlation in 1-d or n-d: n-d shown here
	//   also note that parallel versions exists for speedy processing
	
	void example2() {
		
		long[] dims = new long[]{100,100,100};
		
		DimensionedDataSource input =
				DimensionedStorage.allocate(G.HLF.construct(), dims);

		Fill.compute(G.HLF, G.HLF.random(), input.rawData());
		
		DimensionedDataSource output =
				DimensionedStorage.allocate(G.HLF.construct(), dims);

		ProcedurePaddedDimensionedDataSource padded =
				new ProcedurePaddedDimensionedDataSource<>(G.HLF, input, new ZeroNdOOB<>(G.HLF, input));
		
		
		DimensionedDataSource filter =
				DimensionedStorage.allocate(G.HLF.construct(), new long[] {3,3,3});

		Float16Member value = G.HLF.construct();
		
		IntegerIndex index = new IntegerIndex(dims.length);
		
		index.set(0, 1);
		index.set(1, 1);
		index.set(2, 1);
		value.setV(3);
		filter.set(index, value);
		
		index.set(0, 2);
		index.set(1, 1);
		index.set(2, 2);
		value.setV(-4);
		filter.set(index, value);
		
		index.set(0, 0);
		index.set(1, 1);
		index.set(2, 2);
		value.setV(12);
		filter.set(index, value);
		
		CorrelateND.compute(G.HLF, filter, padded, output);
	}

	// convolution in 1-d or n-d: n-d shown here
	//   also note that parallel versions exists for speedy processing
	
	void example3() {
		
		long[] dims = new long[]{100,100,100};
		
		DimensionedDataSource input =
				DimensionedStorage.allocate(G.HLF.construct(), dims);

		Fill.compute(G.HLF, G.HLF.random(), input.rawData());
		
		DimensionedDataSource output =
				DimensionedStorage.allocate(G.HLF.construct(), dims);

		ProcedurePaddedDimensionedDataSource padded =
				new ProcedurePaddedDimensionedDataSource<>(G.HLF, input, new ZeroNdOOB<>(G.HLF, input));
		
		
		DimensionedDataSource filter =
				DimensionedStorage.allocate(G.HLF.construct(), new long[] {3,3,3});

		Float16Member value = G.HLF.construct();
		
		IntegerIndex index = new IntegerIndex(dims.length);
		
		index.set(0, 1);
		index.set(1, 1);
		index.set(2, 1);
		value.setV(3);
		filter.set(index, value);
		
		index.set(0, 2);
		index.set(1, 1);
		index.set(2, 2);
		value.setV(-4);
		filter.set(index, value);
		
		index.set(0, 0);
		index.set(1, 1);
		index.set(2, 2);
		value.setV(12);
		filter.set(index, value);
		
		ConvolveND.compute(G.HLF, filter, padded, output);
	}

	// resampling : multiple algorithms exist. There are the equivalent of nearest neighbor resampling,
	//   bilinear resampling, and bicubic resampling. The major difference is that they work in n-d
	//   space.
	
	@SuppressWarnings("unused")
	void example4() {
		
		DimensionedDataSource input =
				DimensionedStorage.allocate(G.HP.construct(), new long[] {1000,1000,1000});
		long[] newDims = new long[] {330,542,861};
		DimensionedDataSource output1 = ResampleNearestNeighbor.compute(G.HP, newDims, input);
		DimensionedDataSource output2 = ResampleAveragedLinears.compute(G.HP, newDims, input);
		DimensionedDataSource output3 = ResampleAveragedCubics.compute(G.HP, newDims, input);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy