CompSci 251 Programming Assignment 1 | Complete Solution
- ExpertT
- Rating : 109
- Grade : A+
- Questions : 1
- Solutions : 1026
- Blog : 0
- Earned : $53187.54
CompSci 251: Intermediate Computer Programming
Summer 2015
Programming Assignment 1
1 Overview
For this assignment, you are asked to filter an array. Filtering is a simple process in Electrical Engineering used in image and audio processing.
A lter is a mathematical function that modies the pixels of an image or a stream of sound samples. It's called a filter because the most common purpose for a filter is to remove some `bad' aspect of the image or sounds (e.g. noise, very high sounds, or very low sounds).
1.1 Signals and Filters
For our purposes, signals and filter will be represented as arrays of doubles. In practice, a signal array would be very large (a twenty- ve second clip of 44,100Hz audio would require an array with over a million elements).
Filters are usually a small array with an odd number of doubles. We can apply a lter to a signal to obtain an array of cleaned values. The process is explained below.
0 1 2 3 4
0.00897 0.01530 0.38730 0.65415 0.70597
Figure 1: An example signal.
0 1 2
0.25 0.5 0.25
Figure 2: An example filter.
To produce the filtered signal, we must apply the filter f to (most of) the indices in the dirty signal s. For
example, to compute the cleaned value in index 2, we center the lter over the index 2 in the signal, and
sum products of the overlapping values (s1 f0, s2 f1, and s3 f2). We can repeat this conversation for
three indices of our dirty signal:
c1 = 0:00897 :25 + 0:01530 :5 + 0:38730 :25
c2 = 0:01530 :25 + 0:38730 :5 + 0:65415 :25
c3 = 0:38730 :25 + 0:65415 :5 + 0:70597 :25
Notice that if we try to center the lter over indices 1 or 4, part of our lter falls o the signal. We are unable to lter these indices, so our cleaned signal will be a bit smaller than our original dirty signal.
2 Requirements
Your program should be structured to do the following tasks:
1. Prompt the user for the size of a filter. You should re-prompt the user until you receive valid input (the size of a filter must be a positive, non-zero, odd integer). Your program should not crash by inputting non-integers.
2. Create a filter (as a double array of the user-indicated size) and ask the user, for each index, the value of the filter at that index. You should re-prompt the user until you receive valid input for each index (the value of the filter at each index should be a double between zero and one). Your program should not crash by inputting non-doubles.
3. Apply the filter to the signal.
4. Print the original signal and the filtered signal side-by-side.
You should use the following array declaration as the original signal. It saves you a step of requesting it from the user (which is tedious to input). double noisyArray[] = {
0.008976173, 0.015300936, 0.387302890, 0.654154670, 0.705971749, 1.307427486,
1.071969875, 1.113588720, 1.688798266, 1.334709476, 2.404115760, 2.310886173,
2.432582514, 2.174252365, 2.727890154, 3.222288922, 3.432658520, 3.823261752,
3.184157161, 3.933609629
};
Your output should look as close to the sample runs (following pages) as possible. The table you output should have the same information on the same lines, but is not required to look identical. For example, I used printf to create a nicely-aligned table. Because many of you may not be comfortable with printf (and this is acting as a review assignment), you are not required (but are more than welcome) to use it. You must define the following method which takes in a signal and a filter array and returns a new array which is the filter applied to the signal. If the filter is too big for the filtered signal to contain any elements, return a zero-element array (new double[0]). This loop will require two loops (one nested), and must not do any input or output. You are welcome to define additional methods not listed here. public static double[] applyFilter(double[] values, double[] filter)
3 Submission
Submit your Java le to the D2L dropbox before the posted deadline.
3.1 Sample Run 1
Enter size of filter: 3
Enter value for cell 1 [0-1]: .33333
Enter value for cell 2 [0-1]: .33333
Enter value for cell 3 [0-1]: .33333
0: 0.00898, --------
1: 0.01530, 0.13719
2: 0.38730, 0.35225
3: 0.65415, 0.58247
4: 0.70597, 0.88918
5: 1.30743, 1.02845
6: 1.07197, 1.16432
7: 1.11359, 1.29144
8: 1.68880, 1.37902
9: 1.33471, 1.80919
10: 2.40412, 2.01655
11: 2.31089, 2.38250
12: 2.43258, 2.30588
13: 2.17425, 2.44488
14: 2.72789, 2.70812
15: 3.22229, 3.12758
16: 3.43266, 3.49270
17: 3.82326, 3.47999
18: 3.18416, 3.64697
19: 3.93361, --------
3.2 Sample Run 2
Enter size of filter: 6
Enter size of filter: 5
Enter value for cell 1 [0-1]: .1
Enter value for cell 2 [0-1]: .2
Enter value for cell 3 [0-1]: 5
Enter value for cell 3 [0-1]: .4
Enter value for cell 4 [0-1]: .2
Enter value for cell 5 [0-1]: .1
0: 0.00898, --------
1: 0.01530, --------
2: 0.38730, 0.36031
3: 0.65415, 0.61259
4: 0.70597, 0.82063
5: 1.30743, 1.05533
6: 1.07197, 1.15247
7: 1.11359, 1.26180
8: 1.68880, 1.51279
9: 1.33471, 1.69491
10: 2.40412, 2.10290
11: 2.31089, 2.24259
12: 2.43258, 2.38326
13: 2.17425, 2.45511
14: 2.72789, 2.75699
15: 3.22229, 3.12078
16: 3.43266, 3.37338
17: 3.82326, 3.56826
18: 3.18416, --------
19: 3.93361, --------
[Solved] CompSci 251 Programming Assignment 1 | Complete Solution
- This solution is not purchased yet.
- Submitted On 29 Jun, 2015 09:11:52
- ExpertT
- Rating : 109
- Grade : A+
- Questions : 1
- Solutions : 1026
- Blog : 0
- Earned : $53187.54