<< Chapter < Page Chapter >> Page >

Rewrite the filtering operation filtra() of the Sound Chooser presented in the module Media Representation in Processing in such a way that it implements the FIR filter whose frequency response is represented in [link] . What happens if the filter is applied more than once?

//filtra = new functionvoid filtra(float[] DATAF, float[]DATA, float a0, float a1) { for(int i = 3; i<DATA.length; i++){ DATAF[i]= a0*DATA[i]+a1*DATA[i-1]+a0*DATA[i-2];//Symmetric FIR filter of the second order} }

By writing a for loop that repeats the filtering operation a certain number of times, one canverify that the effect of filtering is emphasized. This intuitive result is due to the fact that, as far as thesignal is concerned, going through m filters of order N (in our case N 2 ) is equivalent to going through a single filter of order m N

Got questions? Get instant answers now!

Considered the Processing code of the blurring example contained in the Processing examples , modify it so that it performs a Gaussian filtering.

// smoothing Gaussian filter, adapted from REAS: // http://processing.org/learning/topics/blur.htmlsize(200, 200); PImage a; // Declare variable "a" of type PImagea = loadImage("vetro.jpg"); // Load the images into the program image(a, 0, 0); // Displays the image from point (0,0)int n2 = 5/2;int m2 = 5/2; int[][] output = new int[width][height];float[][]kernel = { {1, 4, 7, 4, 1}, {4, 16, 26, 16, 4},{7, 26, 41, 26, 7}, {4, 16, 26, 16, 4},{1, 4, 7, 4, 1} };for (int i=0; i<5; i++) for (int j=0; j<5; j++) kernel[i][j] = kernel[i][j]/273;// Convolve the image for(int y=0; y<height; y++) { for(int x=0; x<width/2; x++) { float sum = 0;for(int k=-n2; k<=n2; k++) { for(int j=-m2; j<=m2; j++) { // Reflect x-j to not exceed array boundaryint xp = x-j; int yp = y-k;if (xp<0) { xp = xp + width;} else if (x-j>= width) { xp = xp - width;} // Reflect y-k to not exceed array boundaryif (yp<0) { yp = yp + height;} else if (yp>= height) { yp = yp - height;} sum = sum + kernel[j+m2][k+n2] * red(get(xp, yp));} }output[x][y]= int(sum); }}// Display the result of the convolution // by copying new data into the pixel bufferloadPixels(); for(int i=0; i<height; i++) { for(int j=0; j<width/2; j++) { pixels[i*width + j]= color(output[j][i], output[j][i], output[j][i]); }} updatePixels();

Got questions? Get instant answers now!

Modify the code of [link] so that the effects of the averaging filter mask and the 1 10 1 1 1 1 2 1 1 1 1 are compared. What happens if the central value of the convolution mask is increased further? Then, try toimplement the median filter with a 3 3 cross-shaped mask.

Median filter: // smoothed_glass// smoothing filter, adapted from REAS: // http://www.processing.org/learning/examples/blur.htmlsize(210, 170); PImage a; // Declare variable "a" of type PImagea = loadImage("vetro.jpg"); // Load the images into the program image(a, 0, 0); // Displays the image from point (0,0)// corrupt the central strip of the image with random noisefloat noiseAmp = 0.1; loadPixels();for(int i=0; i<height; i++) { for(int j=width/4; j<width*3/4; j++) { int rdm = constrain((int)(noiseAmp*random(-255, 255) +red(pixels[i*width + j])), 0, 255);pixels[i*width + j] = color(rdm, rdm, rdm);} }updatePixels();int[][]output = new int[width][height]; int[]sortedValues = {0, 0, 0, 0, 0}; int grayVal;// Convolve the imagefor(int y=0; y<height; y++) { for(int x=0; x<width/2; x++) { int indSort = 0;for(int k=-1; k<=1; k++) { for(int j=-1; j<=1; j++) { // Reflect x-j to not exceed array boundaryint xp = x-j; int yp = y-k;if (xp<0) { xp = xp + width;} else if (x-j>= width) { xp = xp - width;} // Reflect y-k to not exceed array boundaryif (yp<0) { yp = yp + height;} else if (yp>= height) { yp = yp - height;} if ((((k != j)&&(k != (-j))) ) || (k == 0)) { //cross selection grayVal = (int)red(get(xp, yp));indSort = 0; while (grayVal<sortedValues[indSort]) {indSort++; }for (int i=4; i>indSort; i--) sortedValues[i] = sortedValues[i-1]; sortedValues[indSort]= grayVal; }} }output[x][y]= int(sortedValues[2]);for (int i=0; i<5; i++) sortedValues[i] = 0;} }// Display the result of the convolution// by copying new data into the pixel buffer loadPixels();for(int i=0; i<height; i++) { for(int j=0; j<width/2; j++) { pixels[i*width + j]= color(output[j][i], output[j][i], output[j][i]);} }updatePixels();

Got questions? Get instant answers now!

Iir filters

The filtering operation represented by [link] is a particular case of difference equation , where a sample of output is only function of the input samples. More generally, it is possible to construct recursive difference equations, where any sample of output is a function of one or more other output samples.

y n 0.5 y n 1 0.5 x n
allows to compute (causally) each sample of the output by only knowing the output at the previous instant and the input atthe same instant. It is easy to realize that by feeding the system represented by [link] with an impulse, we obtain the infinite-length sequence y
    0.5 0.25 0.125 0.0625 ...
. For this purpose, filters of this kind are called Infinite Impulse Response (IIR) filters. The order of an IIR filter is equal to the number of past output samples that it has to store for processing, asdictated by the difference equation. Therefore, the filter of [link] is a first-order filter. For a given filter order, IIR filters allow frequency responses thatare steeper than those of FIR filters, but phase distortions are always introduced by IIR filters. In other words, thedifferent spectral components are delayed by different time amounts. For example, [link] shows the magnitude and phase responses for the first-order IIR filterrepresented by the difference equation [link] . Called a the coefficient that weights the dependence on the output previous value ( 0.5 in the specific [link] ), the impulse response takes the form h n a n . The more a is closer to 1 , the more sustained is the impulse response in time, and the frequencyresponse increases its steepness, thus becoming emphasizing its low-pass character. Obviously, values of a larger than 1 gives a divergent impulse response and, therefore, an unstable behavior of the filter.

Magnitude and phase response of the iir first-order filter

IIR filters are widely used for one-dimensional signals, like audio signals, especially for real-time sample-by-sampleprocessing. Vice versa, it doesn't make much sense to extend recursive processing onto two dimensions. Therefore, in imageprocessing FIR filters are mostly used.

Resonant filter

In the audio field, second-order IIR filters are particularly important, because they realize an elementary resonator. Giventhe difference equation

y n a 1 y n 1 a 2 y n 2 b 0 x n
one can verify that it produces the frequency response of [link] . The coefficients that gives dependence on the past can beexpressed as a 1 2 r ω 0 and a 2 r 2 , where ω 0 is the frequency of the resonance peak and r gives peaks that gets narrower when approaching 1 .

Magnitude and phase response of the second-order iir filter

Verify that the filtering operation filtra() of the Sound Chooser presented in module Media Representation in Processing implements an IIR resonant filter. What is the relation between r and the mouse position along the small bars?

Got questions? Get instant answers now!

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Media processing in processing. OpenStax CNX. Nov 10, 2010 Download for free at http://cnx.org/content/col10268/1.14
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Media processing in processing' conversation and receive update notifications?

Ask