<p>Graphics and audio editing and processing software often contain functions called "High-Pass Filter" and "Low-Pass Filter".  Exactly what do these do, and what are the algorithms for implementing them?</p>

[{'answer_id': 35531, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 6, 'selected': True, 'text': '<p>Wikipedia:</p>\n\n<ul>\n<li><a href="http://en.wikipedia.org/wiki/High-pass_filter" rel="noreferrer">High-pass filter</a></li>\n<li><a href="http://en.wikipedia.org/wiki/Low-pass_filter" rel="noreferrer">Low-pass filter</a></li>\n<li><a href="http://en.wikipedia.org/wiki/Band-pass_filter" rel="noreferrer">Band-pass filter</a></li>\n</ul>\n\n<p>These "high", "low", and "band" terms refer to <em>frequencies</em>. In high-pass, you try to remove low frequencies. In low-pass, you try to remove high. In band pass, you only allow a continuous frequency range to remain.</p>\n\n<p>Choosing the cut-off frequency depends upon your application. Coding these filters can either be done by simulating RC circuits or by playing around with Fourier transforms of your time-based data. See the wikipedia articles for code examples.</p>\n'}, {'answer_id': 35533, 'author': 'BCS', 'author_id': 1343, 'author_profile': 'https://Stackoverflow.com/users/1343', 'pm_score': 3, 'selected': False, 'text': "<p>They are generally Electrical circuits that tend to pass parts of analog signals. High pass tends to transmit more of the high frequency parts and low pass tends to pass more of the low frequency parts.</p>\n\n<p>They can be simulated in software. A walking average can act as a low pass filter for instance and the difference between a walking average and it's input can work as a high pass filter.</p>\n"}, {'answer_id': 36829, 'author': 'puri', 'author_id': 3388, 'author_profile': 'https://Stackoverflow.com/users/3388', 'pm_score': 3, 'selected': False, 'text': '<p><strong>High-pass</strong> filter lets <em>high</em>-frequency (detailed/local information) <em>pass</em>.<br>\n<strong>Low-pass</strong> filter lets <em>low</em>-frequency (coarse/rough/global information) <em>pass</em>.</p>\n'}, {'answer_id': 82372, 'author': 'Hallgrim', 'author_id': 15454, 'author_profile': 'https://Stackoverflow.com/users/15454', 'pm_score': 5, 'selected': False, 'text': '<p>Here is how you implement a low-pass filter using convolution:</p>\n\n\n\n<pre class="lang-c prettyprint-override"><code>double[] signal = (some 1d signal);\ndouble[] filter = [0.25 0.25 0.25 0.25]; // box-car filter\ndouble[] result = new double[signal.Length + filter.Length + 1];\n\n// Set result to zero:\nfor (int i=0; i &lt; result.Length; i++) result[i] = 0;\n\n// Do convolution:\nfor (int i=0; i &lt; signal.Length; i++) \n  for (int j=0; j &lt; filter.Length; j++)\n    result[i+j] = result[i+j] + signal[i] * filter[j];\n</code></pre>\n\n<p>Note that the example is extremely simplified. It does not do range checks and does not handle the edges properly. The filter used (box-car) is a particularly bad lowpass filter, because it will cause a lot of artifacts (ringing). Read up on filter design.</p>\n\n<p>You can also implement the filters in the frequency domain. Here is how you implement a high-pass filter using FFT:</p>\n\n<pre class="lang-c prettyprint-override"><code>double[] signal = (some 1d signal);\n// Do FFT:\ndouble[] real;\ndouble[] imag;\n[real, imag] = fft(signal)\n\n// Set the first quarter of the real part to zero to attenuate the low frequencies\nfor (int i=0; i &lt; real.Length / 4; i++) \n  real[i] = 0;\n\n// Do inverse FFT:\ndouble[] highfrequencysignal = inversefft(real, imag);\n</code></pre>\n\n<p>Again, this is simplified, but you get the idea. The code does not look as complicated as the math.</p>\n'}, {'answer_id': 171371, 'author': 'Andrew Edgecombe', 'author_id': 11694, 'author_profile': 'https://Stackoverflow.com/users/11694', 'pm_score': 3, 'selected': False, 'text': "<p>Filtering describes the act of processing data in a way that applies different levels of attenuation to different frequencies within the data.</p>\n\n<p>A high pass filter will apply minimal attentuation (ie. leave levels unchanged) for high frequencies, but applies maximum attenuation to low frequencies.</p>\n\n<p>A low pass filter is the reverse - it will apply no attenuation to low frequencies by applies attenuation to high frequencies.</p>\n\n<p>There are a number of different filtering algorithms that are used. The two simplest are probably the Finite Impulse Response filter (aka. FIR filter) and the Infinite Impulse Response filter (aka. IIR filter).</p>\n\n<p>The FIR filter works by keeping a series of samples and multiplying each of those samples by a fixed coefficient (which is based on the position in the series). The results of each of these multiplications is accumulated and is the output for that sample. This is referred to as a Multiply-Accumulate - and in dedicated DSP hardware there is a specific MAC instruction for doing just this.</p>\n\n<p>When the next sample is taken it's added to the start of the series, and the oldest sample in the series is removed, and the process repeated.</p>\n\n<p>The behavior of the filter is fixed by the selection of the filter coefficients.</p>\n\n<p>One of the simplest filters that is often provided by image processing software is the averaging filter. This can be implemented by an FIR filter by setting all of the filter coefficients to the same value.</p>\n"}, {'answer_id': 4548775, 'author': 'Eric Brotto', 'author_id': 385559, 'author_profile': 'https://Stackoverflow.com/users/385559', 'pm_score': 3, 'selected': False, 'text': '<p>Here is a super simple example of a low pass filter in C++ that processes the signal one sample at a time:</p>\n\n\n\n<pre class="lang-cpp prettyprint-override"><code>float lopass(float input, float cutoff) {\n lo_pass_output= outputs[0]+ (cutoff*(input-outputs[0])); \noutputs[0]= lo_pass_output;\nreturn(lo_pass_output);\n}\n</code></pre>\n\n<p>Here is pretty much the same thing, except it\'s high pass:</p>\n\n<pre class="lang-cpp prettyprint-override"><code>float hipass(float input, float cutoff) {\n hi_pass_output=input-(outputs[0] + cutoff*(input-outputs[0]));\n outputs[0]=hi_pass_output;\n return(hi_pass_output);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/35530', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1175/']