Question: Adding noise to a signal using java: Please help me as I have spent many hours trying to figure this out Im trying to add
Adding noise to a signal using java:
Please help me as I have spent many hours trying to figure this out
Im trying to add Guassain noise to the following signal by prompting the user to enter a number as a percentage. The code thats that needs the noise added is below. Please help.
// Import the required libraries import java.util.Random; import java.util.Scanner; import java.awt.*; import java.awt.geom.GeneralPath; import javax.swing.*;
// noise class public class noise extends JPanel { private static final long serialVersionUID = 1L; // wave path GeneralPath wave;
// The constructor public noise() { Dimension d = getPreferredSize(); d.width = 30000; setPreferredSize(d); }
// paint component protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (wave == null) initWave(); g2.setPaint(Color.black); g2.draw(wave); }
private void initWave() { // create the square wave line float w = getWidth(); float h = getHeight(); float approxCycles = w / (2 * 100); // calculate dx for the totalWidth (w-1) float dx = (w - 1) / (int) Math.round(2 * approxCycles); float dy = h / 4; // 100 for a wave height of 200 float step = 2 * dx; int steps = (int) (w / step); wave = new GeneralPath(); float x = 0, y = h / 2; wave.moveTo(x, y); for (int j = 0; j < steps; j++) { wave.lineTo(x, y - dy); x += dx; wave.lineTo(x, y - dy); wave.lineTo(x, y + dy); x += dx; wave.lineTo(x, y + dy); wave.lineTo(x, y); } }
// Here is the main method public static void main(String[] args) { // Create the frame JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(new noise())); f.setTitle("Square wave."); // frame size f.setSize(1400, 400); // frame location f.setLocation(200, 200); f.setVisible(true); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
