-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Add tests for ConvolutionFFT
#5767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2ac17fa
Add tests for `ConvolutionFFT`
Hardvan e2e3370
Update directory
Hardvan 1782259
Fix
Hardvan 0df3a58
Merge remote-tracking branch 'origin/convfft_add_tests' into convfft_…
Hardvan 53818c9
Fix
Hardvan b20f063
Fix
Hardvan 5926a24
Merge branch 'master' into convfft_add_tests
Hardvan 7ee5af6
Update directory
Hardvan 59b050e
Fix
Hardvan ea9f962
Merge remote-tracking branch 'origin/convfft_add_tests' into convfft_…
Hardvan d98e4e5
Merge branch 'master' into convfft_add_tests
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ConvolutionFFTTest { | ||
|
||
/** | ||
* Helper method to create a complex signal from an array of doubles. | ||
*/ | ||
private ArrayList<FFT.Complex> createComplexSignal(double[] values) { | ||
ArrayList<FFT.Complex> signal = new ArrayList<>(); | ||
for (double value : values) { | ||
signal.add(new FFT.Complex(value, 0)); | ||
} | ||
return signal; | ||
} | ||
|
||
/** | ||
* Helper method to compare two complex signals for equality within a small margin of error. | ||
*/ | ||
private void assertComplexArrayEquals(ArrayList<FFT.Complex> expected, ArrayList<FFT.Complex> result, double delta) { | ||
assertEquals(expected.size(), result.size(), "Signal lengths are not equal."); | ||
for (int i = 0; i < expected.size(); i++) { | ||
FFT.Complex expectedValue = expected.get(i); | ||
FFT.Complex resultValue = result.get(i); | ||
assertEquals(expectedValue.real(), resultValue.real(), delta, "Real part mismatch at index " + i); | ||
assertEquals(expectedValue.imaginary(), resultValue.imaginary(), delta, "Imaginary part mismatch at index " + i); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConvolutionFFTBasic() { | ||
double[] a = {1, 2, 3}; | ||
Hardvan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
double[] b = {4, 5, 6}; | ||
ArrayList<FFT.Complex> signalA = createComplexSignal(a); | ||
ArrayList<FFT.Complex> signalB = createComplexSignal(b); | ||
|
||
ArrayList<FFT.Complex> expected = createComplexSignal(new double[] {4, 13, 28, 27, 18}); // Expected output | ||
ArrayList<FFT.Complex> result = ConvolutionFFT.convolutionFFT(signalA, signalB); | ||
|
||
assertComplexArrayEquals(expected, result, 1e-9); // Allow small margin of error | ||
} | ||
|
||
@Test | ||
public void testConvolutionFFTWithZeroElements() { | ||
double[] a = {0, 0, 0}; | ||
double[] b = {1, 2, 3}; | ||
ArrayList<FFT.Complex> signalA = createComplexSignal(a); | ||
ArrayList<FFT.Complex> signalB = createComplexSignal(b); | ||
|
||
ArrayList<FFT.Complex> expected = createComplexSignal(new double[] {0, 0, 0, 0, 0}); // All values should be zero | ||
ArrayList<FFT.Complex> result = ConvolutionFFT.convolutionFFT(signalA, signalB); | ||
|
||
assertComplexArrayEquals(expected, result, 1e-9); | ||
} | ||
|
||
@Test | ||
public void testConvolutionFFTWithDifferentSizes() { | ||
double[] a = {1, 2}; | ||
double[] b = {3, 4, 5}; | ||
ArrayList<FFT.Complex> signalA = createComplexSignal(a); | ||
ArrayList<FFT.Complex> signalB = createComplexSignal(b); | ||
|
||
ArrayList<FFT.Complex> expected = createComplexSignal(new double[] {3, 10, 13, 10}); | ||
ArrayList<FFT.Complex> result = ConvolutionFFT.convolutionFFT(signalA, signalB); | ||
|
||
assertComplexArrayEquals(expected, result, 1e-9); | ||
} | ||
|
||
@Test | ||
public void testConvolutionFFTWithSingleElement() { | ||
double[] a = {5}; | ||
double[] b = {2}; | ||
ArrayList<FFT.Complex> signalA = createComplexSignal(a); | ||
ArrayList<FFT.Complex> signalB = createComplexSignal(b); | ||
|
||
ArrayList<FFT.Complex> expected = createComplexSignal(new double[] {10}); | ||
ArrayList<FFT.Complex> result = ConvolutionFFT.convolutionFFT(signalA, signalB); | ||
|
||
assertComplexArrayEquals(expected, result, 1e-9); | ||
} | ||
|
||
@Test | ||
public void testConvolutionFFTWithNegativeValues() { | ||
double[] a = {1, -2, 3}; | ||
double[] b = {-1, 2, -3}; | ||
ArrayList<FFT.Complex> signalA = createComplexSignal(a); | ||
ArrayList<FFT.Complex> signalB = createComplexSignal(b); | ||
|
||
ArrayList<FFT.Complex> expected = createComplexSignal(new double[] {-1, 4, -10, 12, -9}); | ||
ArrayList<FFT.Complex> result = ConvolutionFFT.convolutionFFT(signalA, signalB); | ||
|
||
assertComplexArrayEquals(expected, result, 1e-9); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.