Skip to content

Commit 5bdc6bc

Browse files
Aseem JainAseem Jain
Aseem Jain
authored and
Aseem Jain
committed
Median filter for signal
1 parent 7c23ff2 commit 5bdc6bc

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package me.premaseem.practiceRepeatation;
2+
3+
/*
4+
5+
Recursive Median Filter
6+
7+
Programming challenge description:
8+
Finally, you get the latest measurements in your scientific experiment. It was not easy to collect that data. A lot of noise is present in the space around your secret laboratory. But you know that it's not hard to improve the quality of the measured signal, applying a median filter to the raw data from analog-to-digital converters.
9+
10+
In standard median filter output is at some point is the median value of N samples around that point. Another way to describe that is a window of size N elements slides over the input signal, element by element. On every step, the median of the elements in the window is written to the output.
11+
12+
The median of the set of integers is the middle element in the sorted list of these numbers.
13+
14+
In recursive median filter output is the median of elements in the current window. But now, in the left half of that window input signal is replaced by output from the previous steps.
15+
16+
17+
Write a program that filters out the noise from the raw data, using the recursive median filter.
18+
19+
For simplicity, assume that the size of the filter window is always odd.
20+
Do not process the boundaries of the input signal, where it's not enough elements to compose a window of the necessary size. In other words, just crop the signal. If the filter's window size is 5 you will lose 2 first and 2 last elements.
21+
Also, use input elements at the beginning of the signal where there are no outputs for the left part of the filter's window.
22+
23+
For example, let's filter signal 17 18 19 37 21 22 23 85 25 26 using a window of size 3. For the first 3 elements 17 18 19 the median is 18. Shifting filter's window to the next 3 elements 18 19 37 gives 19 as an output. 19 37 21 outputs 21, and so on.
24+
25+
Input:
26+
The first line contains an odd integer, the size of the filter's window. Each next line of the input has a comma-separated series of positive integers. That is raw data of the measured signal. For example:
27+
28+
3
29+
5,5,5,15,5,5,5
30+
1,2,3,25,5,6,52,7,8
31+
Output:
32+
Print out a comma-separated series of integers, that is a result of applying the recursive median filter to the raw input data. For example:
33+
34+
5,5,5,5,5
35+
2,3,5,5,6,7,7
36+
37+
===============
38+
Test 1:
39+
3
40+
7,7,7,7,17,7,7,7,7,77,7,7,7
41+
Expected Output
42+
7,7,7,7,7,7,7,7,7,7,7
43+
44+
Test 2:
45+
3
46+
0,87,173,258,342,422,5499,573,642,707,766,819,866,9906,939,965,984,996,1000
47+
0,34,69,104,139,173,207,9241,275,309,342,374,406,438,469,499,529,559,587,615,642,669,694,9719,743,766,788,809,829,848,866,882,898,913,927,9939,951,961,970,978,984,990,994,997,999,1000
48+
49+
Expected Output
50+
87,173,258,342,422,573,573,642,707,766,819,866,939,939,965,984,996
51+
34,69,104,139,173,207,275,275,309,342,374,406,438,469,499,529,559,587,615,642,669,694,743,743,766,788,809,829,848,866,882,898,913,927,951,951,961,970,978,984,990,994,997,999
52+
53+
54+
*/
55+
56+
import java.io.BufferedReader;
57+
import java.io.IOException;
58+
import java.io.InputStreamReader;
59+
import java.nio.charset.StandardCharsets;
60+
import java.util.ArrayList;
61+
import java.util.Collections;
62+
import java.util.List;
63+
64+
public class Median_filter_signal {
65+
66+
public static void main(String[] args) throws IOException {
67+
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
68+
BufferedReader in = new BufferedReader(reader);
69+
String line;
70+
Integer size = null;
71+
List<Integer> lst = new ArrayList<>();
72+
while ((line = in.readLine()) != null) {
73+
if (size == null){
74+
size = Integer.parseInt(line);
75+
continue;
76+
}
77+
String[] ca = line.split(",");
78+
for(int i=0;i<ca.length;i++){
79+
lst.add(Integer.parseInt(ca[i]));
80+
}
81+
List<Integer> window = new ArrayList<>();;
82+
for(int i=0; i<=lst.size()-size;i++){
83+
window = new ArrayList<>();
84+
for(int j=i; j<i+size;j++){
85+
window.add(lst.get(j));
86+
Collections.sort(window);
87+
}
88+
int left = size/2;
89+
Integer median = window.get(left);
90+
System.out.print(median);
91+
lst.set(i+1,median);
92+
if (i < lst.size()-size){
93+
System.out.print(",");
94+
}
95+
}
96+
97+
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)