<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by M. Balcilar on Medium]]></title>
        <description><![CDATA[Stories by M. Balcilar on Medium]]></description>
        <link>https://medium.com/@balcilar?source=rss-4dff2fe6d340------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hVOEcumpl-7uzWgYkBim0A.jpeg</url>
            <title>Stories by M. Balcilar on Medium</title>
            <link>https://medium.com/@balcilar?source=rss-4dff2fe6d340------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:58:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@balcilar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Spectral Feature of Graph Signals]]></title>
            <link>https://balcilar.medium.com/spectral-feature-of-graph-signals-a54a244bab22?source=rss-4dff2fe6d340------2</link>
            <guid isPermaLink="false">https://medium.com/p/a54a244bab22</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[signal-processing]]></category>
            <category><![CDATA[graph]]></category>
            <dc:creator><![CDATA[M. Balcilar]]></dc:creator>
            <pubDate>Wed, 03 Apr 2019 13:20:24 GMT</pubDate>
            <atom:updated>2019-04-03T13:20:24.483Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@muhammetbalcilar/struggling-signals-from-graph-34674e699df8">In the previous post</a>, Graph signals, its Laplacian matrix, and some exiting features were represented. In this post, two main Spectral features of a graph and parametric filter localization of graph signal will be represented.</p><h4>Spectral Filtering is Independent to the Dummy Nodes</h4><p>If we want to make all given graphs have the same nodes, we need to add some dummy nodes which has no connection to other nodes. Spectral filtering is independent of that newly added dummy nodes. Does not matter how many nodes we add. The filtering works exactly the same.</p><p>To test this feature, the same graph and filter from the previous post are used. In addition to that 100 nodes graph, 100 dummy nodes which located (0,0) position without any connection to other nodes were created. Then the same filter applied on the same signal. The result is in the following figure. Also, that demo is in the <a href="https://github.com/balcilar/Graph_Signal_Processing">demo5.m</a> script file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9s-EIt_RUwBIJckKiPCOCA.png" /><figcaption>Fig1. Filtering results on the original graph and dummy nodes added graph</figcaption></figure><p>Since all dummy nodes located in the same position, all 100 dummy node are seen in the same point which has no connection to other nodes in the top right figure. At the bottom figure, the node’s signal value for both two graphs is represented. First 100 nodes value (the original nodes) are the same, last 100 node’s (dummy nodes) signal are all zero.</p><h4><strong>Spectral Filtering is Independent to the Order of the Nodes</strong></h4><p>This feature is very important for graph signal processing. Hence the graph cannot carry any information by the order of the nodes but just their connections. So valid Graph filtering must not change if the order of the nodes is changed. Which means does not matter which node is the first, which nodes is the second, so on so forth. To test this feature, we created a <a href="https://github.com/balcilar/Graph_Signal_Processing">demo6.m</a> script. Within that demo, we created 100 dummy node like previous nodes then we randomly reordered all 200 nodes and created a new graph. Since the result is exactly the same with above Fig1 we do not put the result here.</p><h3>Filter Localization</h3><p>When the filters are to be learned like in convolution neural network, we need to localize filter, which means we somehow make sure that the same filter must be applied all over the signals which come from different graphs. When <em>U</em> and λs are eigenvector and eigenvalues of Laplacian of the given graph, <em>f</em> is the filter function respect to the eigenvalue, <em>s</em> is the input signal and <em>sf</em> is the filtered signal, the spectral filtering on graph signal can be written as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*CI0VsuCWDNhXP0CSNvLXbA.jpeg" /></figure><p>In that equation, <em>f</em> function is the one which is to be learned. All other variables depend on the given signal and graph. Note the number of node <em>n</em>, the dimension of <em>U</em>, the dimension of <em>s</em> can be changed for the different graph. Do not need to say even there is the same number of node since the graphs are different, eigenvectors and eigenvalues are can be different as well.</p><p>The first idea comes into mind is to define <em>f</em> function by the independent coefficient for each given eigenvalue, which means <em>f(Λ)</em> function consists of <em>n</em> length vector. Every number in that vector represent the coefficient of the corresponding eigenvector if all number of nodes get the same by adding dummy nodes, that idea seems possible. In that point localization problem appears. Suppose the last eigenvalue of the first signal is 5, but the last eigenvalue of the second signal is 10. That means the same coefficient (last element of <em>f</em> vector) will be the coefficient of the different frequency component which is totally wrong. Convolutional neural network theory tells us to apply the same effect of the filter ( or we can say coefficients which are shared) for different train set samples and finally find that common, shared filters coefficients.</p><p><strong>Filter Localization by Polynomial Filters</strong></p><p>To solve the filter localization problem, polynomial filters which represent the filter coefficient by the polynomial summation of given eigenvalue can be used. The following formula expresses that representation where <em>K</em> is the degree of the polynomial.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/219/1*aYQAXUyQ_bM97J2S6A-NEw.jpeg" /></figure><p>Also, we can reformulate it by matrix multiplication where <em>C</em> matrix is the power matrix of eigenvalues as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*lINpaBDdAOwN7SnmgdLmSg.jpeg" /></figure><p>Now regardless of the eigenvalues of the graph, learned common coefficients αs are used for calculating the coefficient of filter for each basis function. Within that representation, the filter is modeled by <em>K+1</em> number of coefficients. Let’s say our filter has <em>K+1</em> degree of freedom which has to be learned.</p><p>To demonstrate that filtering, we used predefined 50 and 100 nodes graph. the first node of each graph set as 1 and the rest of them are 0 as initially given so-called Dirac signal. <em>K=15</em> coefficient is used. Polynomial filter coefficients were calculated to give the same result of the standard filter in the previous demo where <em>flt =exp(-20*v) f</em>or 100 nodes graph. All codes are given in <a href="https://github.com/balcilar/Graph_Signal_Processing">demo7.m</a>. In that demo, we applied the same filter on graph1 and then reconstruct polynomial coefficient by inverse calculation. We called that calculated coefficient as learned parameters. Then we applied that learned parameters on graph2. We also compared that learned filter result with standard filtering using the given filter. The result is quite the same under some error margin because of using just <em>K=15</em> coefficient.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wHH5A7KX61gGVsNGMwKksQ.jpeg" /><figcaption>Fig2. Polynomial Filter Result on Graph2, by the learned coefficient from graph1 and standard way.</figcaption></figure><p><strong>Filter Localization by B-Spline</strong></p><p>Hence we represented the filter by the sum of power series of the concerned eigenvalue, the filter result is changed very dramatically for small changes of higher order coefficient. That means the filter is too sensitive to high order coefficient of the filter. That makes learning those coefficients difficult. another smart way to localize filter is resampling the eigenvalues over a common discrete basis (Let’s say resampling it over λ*=0, 0.2, 0.4, …,2.0) and rewrite the eigenvalues of that newly created eigenvalues by the weighted sum of original eigenvalues. That solution gets the idea of B-spline into mind immediately. Because B-spline is a spline function of given degree which can be expressed as a <a href="https://en.wikipedia.org/wiki/Linear_combination">linear combination</a> of B-splines of that degree. Roughly speaking b-spline analysis gives us linear transformation matrix B which transform original eigenvector and eigenvalues into desired eigenvalues and its corresponding eigenvectors. Assume coefficient of b-spline shown by <em>nxm</em> dimensional <em>B</em> matrix, and the coefficient vector to be learned is shown by α, the filter Λ can be defined as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*SVg5zpgr__xNFku9RCrABA.jpeg" /></figure><p>Let’s test that idea on the graph in the first demo in the previous post. In that demo, the graph consists of 101 nodes and each node connected to previous and next nodes like one big circle. That graph’s maximum eigenvalue is 4. But we wanted to obtain the eigenvector of 21 valued eigenvalues where λ*=0, 0.1, 0.2, …,2.0. Here are some initial eigenvector and b-spline transformed one. You can find that code in the <a href="https://github.com/balcilar/Graph_Signal_Processing">demo8.m</a> script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LQVuzG7pvQOE7u4roXTOkQ.jpeg" /><figcaption>Fig3. Original and spline basis transformed eigenvectors</figcaption></figure><p>The above figure shows 5th,11th and last original eigenvectors on the first row, and spline basis transformed ones on the second row.</p><p>There are just two parameters of b-spline transformation. These are the number of bases which we chose 21 (it is the degree of freedom, number of parameters to be learned) in the above demo and maximum eigenvalues which we selected it 2. Within that way, we just need to learn the coefficient of transformed eigenvectors.</p><p>So, let’s do the filtering on the second graph by the learned b-spline coefficient from the first graph. <a href="https://github.com/balcilar/Graph_Signal_Processing">demo9.m</a> is prepared for this aim. We again calculated the b-spline filter coefficients from graph1 where it gives the same result by <em>flt =exp(-20*v).</em> Then we applied that learned coefficient to graph2 signal. To do comparison we also applied the standard filter to that graph signal as well. We used 50 discrete point eigenvalues from 0 to 8 which means the learned coefficient vector consists of 50 numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d2pKRWHPyi-y4IEMiDf2JQ.jpeg" /><figcaption>Fig4. B-spline Filter Result on Graph2, by the learned coefficient from graph1 and standard way.</figcaption></figure><p>To make sure that both filtering on original eigenvector basis and transformed basis are the same, <a href="https://github.com/balcilar/Graph_Signal_Processing">demo10.m</a> script is prepared. In this demo, we presented the same filtering with demo5 on both original basis and spline transformed basis by 25 coefficient. Finally here is the signal level of nodes for both two methods as a comparison.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N6wIPfGU8NI38FtZnpPCkg.jpeg" /><figcaption>Fig5. Filtered signal values of nodes over different basis</figcaption></figure><p><strong>Filter Localization by Chebyshev Expansion</strong></p><p>So far, b-spline approach sounds very good. However, if the graphs are a big graph where the number of nodes far more bigger than the b-spline coefficient, because of eigenvector multiplication complexity, it becomes less efficient in terms of computational time. In this case, we also can use Chebyshev expansion which is the sequence of orthogonal polynomials defined by recursive relation instead of eigenvectors. Roughly speaking, instead of calculation of eigenvector and eigenvalues, we can write any graph’s signal’s orthogonal basis recursively. The number of basis depends on our choice and this number will be the number of parameters to be learned.</p><p>According to the method, first, the Laplacian of the graph must be normalized where its eigenvalues are in <em>[-1 1]</em>. Then the desired number of basis can be created according to given recursive equations. Finally, filtered signal <em>sf</em> can be calculated by multiplying that basis matrix and filter coefficient α. Since there is <em>K+1</em> number of basis, the filter vector α consist of <em>K+</em>1 coefficient which is to be learned. When <em>L</em> is given laplacian of graph, <em>I </em>is eye matrix with the same size of <em>L</em>, <em>s</em> is signal on that graph, K is the desired number of basis of the filter, α is coefficient of the filter and <em>sf</em> is the filtered signal, Chebyshev filtering can be shown by following equations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*6AI0x2of4d1DymYh-s65Zw.jpeg" /></figure><p>We prepared the same manner demo named demo11.m. Again we learned Chebyshev filter coefficient from graph1 by <em>flt =exp(-20*v).</em>Then we used that coefficient on filtering graph2 signals. To make a comparison standard filtering results of graph2 was represented as well in the following figure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W49OzAy1KC1x0rEO9ZBi_A.jpeg" /><figcaption>Fig6. Chebyshev Filter Result on Graph2, by the learned coefficient from graph1 and standard way.</figcaption></figure><h3>General View of Filtering</h3><p>As a result, we have 4 options for filtering. Direct filtering, polynomial filtering, b-spline filtering, and Chebyshev filtering. Here is the general formula of each filter respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*tWfhJrCsFqfAlfHX_ApBfg.jpeg" /></figure><p>In all formula <em>W</em> refers coefficient which is to be learned, <em>s</em> is input signal, <em>sf</em> is output signal, <em>U</em> is eigenvectors. In the first formula which is ideal filtering for the given graph and its signal, <em>W</em> coefficient matrix consist of the number of node variable and each element is the coefficient of the corresponding eigenvalue. that is why the first one is the ideal filter but not useful on the neural network because of filter localization problem. In the rest of the filters, <em>W</em> coefficient vector consist of <em>K</em> number of variables and each variable is more or less for the same frequency component regardless of the graph.</p><h3><strong>What is Next?</strong></h3><p>Now we are ready to create Spectral-Based Graph Convolutional Neural Network for various signals which come different graphs.</p><h3>Note</h3><p>This research was funded by LITIS Lab at University of Rouen-Normandy. It is part of AGAC Project (Projet Régional RIN Recherche sur l’analyse de graphes et ses applications à la chemoinformatique).</p><p>You can find the demo code on my <a href="https://github.com/balcilar/Graph_Signal_Processing">GitHub </a>profile.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a54a244bab22" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Struggling Signals from Graph]]></title>
            <link>https://balcilar.medium.com/struggling-signals-from-graph-34674e699df8?source=rss-4dff2fe6d340------2</link>
            <guid isPermaLink="false">https://medium.com/p/34674e699df8</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[signal-processing]]></category>
            <category><![CDATA[graph-signal-processing]]></category>
            <category><![CDATA[graph]]></category>
            <dc:creator><![CDATA[M. Balcilar]]></dc:creator>
            <pubDate>Fri, 01 Mar 2019 16:48:55 GMT</pubDate>
            <atom:updated>2019-03-04T16:57:06.879Z</atom:updated>
            <content:encoded><![CDATA[<h3>Struggling Signals on Graph</h3><h3><strong>Introduction</strong></h3><p>Every engineer is too familiar with classical signal processing theory where the signal comes from Euclidian domain like 1d time domain as speech, financial data,…etc or 2d spatial domain as images or even 3d spatial domain as point clouds. Within that fundamental signal processing literature, we can tackle the problems on to that euclidian domain data and figure out some meaningful results. For instance prediction of tomorrow’s USD/EUR parity, understanding if the given image includes some certain objects, determine the 3D model of the environment using laser scanners for mobile robots…etc. All we need to do apply either some basic or more sophisticated signal processing algorithm on to those signals. But what if our signals come from a non-euclidian domain?</p><p>First of all, we need to clarify the meaning of Euclidian domain. I am not meant the mathematical definition but just an engineer point of view. The euclidian domain is a rigid space where each signal is sensed from equally sampled space even it is 1d,2d,3d or even nd. But non-euclidian domain is vice versa. There is no regular sample point, all sample point might be arbitrary. The connection of sampled points might be completely arbitrary, no rules there is!. Here is a sample signal on 2D euclidian domain and non-euclidean domain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*PrdL5f_cs0ZLNwijdCNZ-A.png" /><figcaption>Fig1. Sample signal on Euclidian and Non-Euclidian Domain, taken from [1]</figcaption></figure><p>To represent 1D Euclidian domain, we can use just an array, or 2D just a matrix grid is sufficient. Since the non-euclidean domain has no regularity, we cannot represent it by an array or grid matrix. In that point, Graph, the most beautiful data structure in computer science, helps us. Since we can represent both euclidian or non-euclidian domain using Graphs, signal processing on graphs becomes the general form of signal processing literature.</p><p>In this series, I am going to show some fascinating features of Graph Signal Processing, applying a low-pass filter on both 1D and 2D euclidian domain signal by classical signal processing and also Graph signal processing to compare both results are the same. Within that way, we will validate the working mechanism of Graph Signal Processing. In addition to this, I will show you an example of filtering a signal on the non-euclidian domain.</p><h3><strong>Graph</strong></h3><p>Mathematical speaking, Graph can be represented as G=(V,E) where pair of V and E represents nodes and edges respectively. In the computer science point of view, V is the list of nodes where our discrete signal comes from, E is the list connections which represent spatial relations between either two nodes or more than two nodes (if more than two nodes are connected by one edge, we call that graph as hypergraph). The spatial relation, that the edge represents, might be just a logical variable like connected or not (weightless graph) or a positive real number for representing the weight (or sometimes distance) of the concerned nodes (weighted graph). If the connections represent one-way relation like i→ j, we call it a directed graph, but for two-way relation like i↔j , it is an undirected graph. Except for hypergraph, regardless of the graph weighted or not, or directed or not, we can define it by square matrix W, where it’s row and cols are equal to the number of nodes. If the matrix is weighted, then the value of corresponding cells should be a positive real number unless just 1s or 0s (in this time we call Weight matrix W as the adjacency matrix as well). If the graph is undirected than W matrix should be symmetric. Here is the definition of an undirected graph where each node has just connections to its precedent and ancient node. None of the nodes has no self-connection (diagonal are all zero) and the first node’s ancient node is the last node, the last node’s precedent node is the first node (like a closed loop).</p><pre>% create time series grid as graph</pre><pre>n=101;<br>W=zeros(n,n);<br>for i=1:n-1<br>  W(i,i+1)=1;<br>  W(i+1,i)=1;<br>end<br>W(1,n)=1;<br>W(n,1)=1;</pre><h3><strong>Laplacian Matrix of Graph</strong></h3><p>As far as I know, that matrix is the best way of representing the concerned graph because of its some fascinating features. Denote the degree of node i (total number of connection) by d(i) and let D=diag(d(1),d(2),…,d(n)) be the diagonal matrix of node’s degrees, the Laplacian matrix is;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*dVUOGXHSBFhgIojvQaxPbg.png" /></figure><p>Where W is a weight adjacency matrix where the values 0s or positive real number. If the graph is not weighted than that matrix consist of just 0s and 1s. Here is the code for calculation of normalized Laplacian matrix of the given graph by W.</p><pre>% calculate Laplacian Matrix<br>d = sum(W,2);<br>L = diag(d)-W;</pre><h3>EigenVectors and EigenValues of Laplacian Matrix</h3><p>Spectral theorem tells that for every symmetric real numbered matrix such as Laplacian matrix L, there exists an orthogonal matrix U (columns are eigenvectors of L) which provides below equation [2].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*fW0yM_iOc8F0aWTzujbw0g.png" /></figure><p>where λ ‘s are the corresponding eigenvalue of the eigenvectors. Let’s assume v is one of the columns of U which means it is one of the eigenvectors of L. Than it is not difficult to show the below equation in case of the unweighted graph[3].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/1*bZaSaYaTCnAXz-HJZcs95Q.png" /></figure><p>In the case of the weighted graph, the formula becomes as follows[5].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*QzizyDv1E_jva0gfffO7MA.png" /></figure><p>That equation is the most important point of Graph Embedding. Hence that formula tells us; if we assign the values of the eigenvector, whose eigenvalue is minimum, to the nodes as a value then the connected nodes value become as much as similar to each other. That is because of the left side of the formula which represents the square error of the connected node’s value (In case of the weighted graph the weights become weights of the square error). But if we assign the same value to every node, then the error becomes absolutely zero which we can obtain the trivial solution which is not our main target. Hence that formula tells us the error is also equal to the concerned eigenvalue, which means all eigenvalues must be positive and the one who is zero gives us a trivial solution where its eigenvector all consist of the same values. Hands-on again, Let’s calculate eigenvalues and eigenvectors of given Laplacian matrix and plot first three(smallest eigenvalues) eigenvectors.</p><pre>% calculate basis<br>[u v]=eig(L);</pre><pre>figure;<br>subplot(1,3,1);plot(u(:,1));ylim([-0.15 0.15]);xlim([1 101]); title([&#39;1st eigenvector, value=&#39; num2str(v(1,1))]);</pre><pre>subplot(1,3,2);plot(u(:,2));ylim([-0.15 0.15]);xlim([1 101]); title([&#39;2nd eigenvector, value=&#39; num2str(v(2,2))]);</pre><pre>subplot(1,3,3);plot(u(:,3));ylim([-0.15 0.15]);xlim([1 101]); title([&#39;3th eigenvector, value=&#39; num2str(v(3,3))]);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KWJLXbDECFosyDSvml0xNw.png" /><figcaption>Fig2. First three eigenvectors of the given graph for n=101</figcaption></figure><p>As you can see the eigenvector whose eigenvalue is zero gives a trivial solution as all nodes have the same value where the reason is obvious. But an interesting point is the eigenvector whose value is not zero but again as minimum as possible. For instance, as you can see 2nd and 3rd eigenvector’s values which change slightly from one node to its precedent node. Since we connected the first node to the last, also the last value and the first value of that eigenvector are just slightly different from each other which makes the square error minimum. The error values of 2nd and 3rd eigenvector (eigenvalues) are also the same for our case which is 0.001934.</p><p>This fact is the main logic behind the embedding the graph, which means map the node of the graph in some lower dimensional space such as 2D. So if we want to put those nodes into some location in 2D space, we should assign the minimum non-zero eigenvalue’s eigenvector for the 1st axis, the second minimum non-zero eigenvalues&#39; eigenvector for the 2nd axis. The question is; what if we assign the maximum valued eigenvectors as locations of the nodes?</p><p>Here is the code for assigning the mentioned values to the coordinate of nodes and visualize it. Note we used GSPBox [4] to illustrate the graph and we just represent 21 nodes graph in order to avoid the complexity of figure.</p><pre>%% show graph and signal<br>run ../gspbox/gsp_start<br>coor=u(:,2:3);<br>G=gsp_graph(W,coor);<br>figure;gsp_plot_graph(G)</pre><pre>coor=u(:,end-1:end);<br>G=gsp_graph(W,coor);<br>figure;gsp_plot_graph(G)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iicf1KTahD64FNXzunAmVQ.png" /><figcaption>Fig3. Location of the nodes by 2nd and 3rd eigenvector, and n-1 and nth eigenvector for the same graph n=21</figcaption></figure><p>The embedding of the given time series graph using the first two non-zero valued eigenvectors amazingly makes sense. Putting all nodes around the circle nearby near is the best solution for the graph where the nodes just connected with its precedent and ancient nodes. As you guess using two eigenvectors whose eigenvalues are maximum is not good at all, vice versa it is the worst embedding. Again the nodes are located around the circle but connected nodes are as much as far away from another node. Now let’s have a look at all eigenvectors and their measured frequency for the same graph but n=101.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*quqa2fHrDQTPOLpfe5yVXg.gif" /><figcaption>Fig4. All eigenvectors for the given graph for n=101</figcaption></figure><h3><strong>Fourier Transform versus Eigenvector Transform</strong></h3><p>Thanks to the Discrete Fourier Transform (DFT), we can rewrite any signal (1d,2d or nd having some special shape or just a noise does not matter) with the weighted sum of basis sinus signals (0Hz, 1Hz, 2Hz,….n/2Hz of sinusoidal signals for 1d case). The weight of each basis signal is a complex number where the magnitude of that weight shows the contribution of the basis signal, but the angle of that weight shows the phase of the basis signal. For the case of n=101 nodes time series signal, DTF result consists of 51 complex numbers (0hz to 50hz) the first coefficient represents the contribution of 0Hz sinus signal, but the last coefficient represents 50Hz sinus signal.</p><p>Interestingly, in Fig4, we can see that first eigenvector is just a line and its frequency is just 0. Second and third eigenvector are just 1 Hz sinusoidal signal but has 90-degree phase differ. Forth and fifth eigenvectors are 2Hz sinusoidal but have again 90-degree phase differ. And last two eigenvectors are 50Hz sinusoidal signal again 90-degree phase differ. So this fact encourages us to write the signal on the graph as the weighted sum of those bases (eigenvectors). But the coefficients must be a real number instead of complex in classical Fourier transform. Because we cannot guaranty that basis are perfectly sinus signal and we cannot shift them on the time domain in order to create phase difference. So instead of using 51 different sinus signal and change its phase if it is necessary to obtain the given input signal, we can use one 0Hz signal plus pair of signal and its already shifted by 90-degree form for i=1 to 50 (which makes 1+50x2=101 number of basis signal) to rewrite the given input signal. This process can be called Eigenvector Transform. In our case where the graph is a time series, we can know each eigenvector’s frequency. But in general case, we cannot know which frequency can be carried by each eigenvector. Even an eigenvector might carry a mixture of signals, we just know that these signals are an orthogonal basis and even so we can use that basis to rewrite our given input signal by the following equation where s is given signal and f is frequency profile. The second part refers to spatial to frequency transform, but the first part refers to the frequency to spatial transform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*JhZtv7vcKwiCrwO2ds9MdA.png" /></figure><p>In classical Fourier transform, we can represent the result by the magnitude of the basis versus the frequency of the basis and we called it frequency profile of the signal. But in general the graph signal case (not specifically our example for time series graph) we can just be sure that eigenvalues all positive and starts 0 and it monotonically increases. The minimum eigenvalue shows the minimum variation (sum of square differences of connected nodes value) of the value of connected nodes, maximum eigenvalue shows the maximum variation of the value of connected nodes, like how the first component of classical Fourier transform shows minimum frequency basis (just zero frequency stable straight line signal, the bias of the signal), the last component shows the magnitude of maximum frequency basis (the most variating signal which has as much as possible up and down)</p><p>So the best way to show the frequency profile for graph signal is using that extracted coefficient versus eigenvalue. Here is generated random signal s, it’s frequency profile (DFT results) and graph frequency profile.</p><pre>% create arbitrary signal<br>s=randn(size(W,1),1);</pre><pre>% calcualte graph frequency profile<br>f=u&#39;*s;</pre><pre>% calculate classical frequency profile<br>cf=abs(fft(s));<br>cf=cf(1:51);</pre><pre>figure;subplot(1,2,1);plot(v,f,&#39;r*-&#39;);<br>xlabel(&#39;eigenvalues&#39;);ylabel(&#39;coefficient&#39;);<br>title(&#39;Graph Frequency profile&#39;);</pre><pre>subplot(1,2,2);plot(0:50,cf,&#39;b*-&#39;);<br>xlabel(&#39;Frequencies&#39;);ylabel(&#39;magnitude&#39;);<br>title(&#39;Frequency profile&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yX24N225ZreeU_Evx9uHDA.png" /><figcaption>Fig5. Graph Frequency Profile and Classic Frequency Profile of a randomly generated signal</figcaption></figure><p>In classical frequency profile, there are 51 magnitude values (always positive since it is the absolute value of the complex number) where refers 0Hz to 50Hz for n=101 sampling point signal, but also that numbers have angle values as well which we did not plot it on the figure. But Graph Frequency profile consists of 101 real number and some might be negative. Because they are just a coefficient of the corresponding eigenvectors (Note negative coefficient means making the eigenvector signal up-down, which is equivalent of 180-degree phase differences).</p><h3><strong>Filtering on Frequency Domain</strong></h3><p>Classical frequency profile and graph frequency profile does not similar to each other. But we do not expect to get the same profile, hence orthogonal basis is different. But in our special time series graph case, we theoretically know that 1st eigenvectors frequency is 0Hz, 2nd and 3rd is 1Hz, 4th and 5th is 2Hz, and 100th and 101th is 50Hz. So if we create a frequency domain filter for classical signal, we can rewrite that filter coefficient for graph frequency profile and create our flt variable easily. Suppose somehow we write the graph filter flt by the function of eigenvalues, then filtered signal sf can be calculated by the following equation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*LMYGY2zRyyFpQjVu8TnKzQ.png" /></figure><p>Here is the code of filtering the same random signal by classical frequency filter and graph frequency filtering where we use exp(-|f|/10) low pass filter.</p><pre>% create filter for classical signal processing<br>fs=fftshift(fft(s));<br>f=linspace(-(n-1)/2,(n-1)/2,n)&#39;;<br>flt = exp(-abs(f)*0.1);</pre><pre>% apply that filter on to time series signal<br>ot=ifft(ifftshift(flt.*fs));</pre><pre>% create the same filter for graph signal processing<br>flt=[1:(n-1)/2]&#39;;<br>flt=[flt flt]&#39;;<br>flt=[0; flt(:)];<br>flt = exp(-abs(flt)*0.1);</pre><pre>% apply that signal on to graph signal<br>sf=u*(flt.*(u&#39;*s));</pre><pre>% show results<br>figure;hold on;plot(s,&#39;k--&#39;);plot(ot,&#39;b-&#39;,&#39;linewidth&#39;,2);<br>xlim([1 n]);plot(sf,&#39;r-&#39;,&#39;linewidth&#39;,1);<br>xlabel(&#39;node order or time&#39;);plot(ot-sf,&#39;g-&#39;);<br>legend({&#39;original signal&#39;,&#39;graph filtering&#39;,&#39;time series filtering&#39;,&#39;differences&#39;});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*06VxhUm7qciuFeiJUPf6nw.png" /><figcaption>Fig6. A random signal is filtered by classical frequency filter and graph frequency filter</figcaption></figure><p>As a result, as long as we rewrite the filter for graph signals, we take the same results as you can see in the above figure. Actually, we do not need to find the same effect of the classic filter for graph signal in the application. I just did it to show the equivalent of both filtering techniques as a validation step. Normally we can create our own filters according to the eigenvalues. In the beginning, we told that even this signal is ordinary time series, we pretend it as comes from the graph. So here is the input and the filtered signal on the graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_OcBRf_phZmNdIUeu5Y2jA.png" /><figcaption>Fig7. Input and the filtered signal on the graph</figcaption></figure><h3><strong>Graph Signal Filtering for 2D Euclidian Domain</strong></h3><p>So far, we have shown and validated the Graph signal filtering for 1D time series. Now we repeat the procedure for 2D euclidian signals such an image. Here is W matrix definition of the image whose size is 21x21.</p><pre>n=21;<br>W=zeros(n*n,n*n);<br>for i=1:n<br>  for j=1:n<br>    p=sub2ind([n n],i,j);<br>    try<br>      p1=sub2ind([n n],i+1,j);<br>      W(p,p1)=1;<br>      W(p1,p)=1;<br>    catch<br>    end<br>    try<br>      p2=sub2ind([n n],i-1,j);<br>      W(p,p2)=1;<br>      W(p2,p)=1;<br>    catch<br>    end<br>    try<br>      p3=sub2ind([n n],i,j+1);<br>      W(p,p3)=1;<br>      W(p3,p)=1;<br>    catch<br>    end<br>    try<br>      p4=sub2ind([n n],i,j-1);<br>      W(p,p4)=1;<br>      W(p4,p)=1;<br>    catch<br>    end<br>  end<br>end <br>% connect left column to right column top rows to bottom row<br>for i=1:n<br>  p1=sub2ind([n n],1,i);<br>  p2=sub2ind([n n],n,i);<br>  W(p1,p2)=1;<br>  W(p2,p1)=1;<br>  p1=sub2ind([n n],i,1);<br>  p2=sub2ind([n n],i,n);<br>  W(p1,p2)=1;<br>  W(p2,p1)=1;<br>end</pre><p>With the same procedure, we can calculate the Laplacian matrix, its eigenvalue, and eigenvectors.</p><pre>% calculate Laplacian Matrix<br>d = sum(W,2);<br>L = diag(d)-W;</pre><pre>% calculate basis<br>[u v]=eig(L);<br></pre><pre>% make eignevalue as vector<br>v=diag(v);</pre><p>Here are all 21x21=441 eigenvectors and their dominant x and y-direction frequencies at the below figure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*dUv2vRdALPNHhktJ660PNg.gif" /><figcaption>Fig8. All eigenvectors for a graph representation of 21x21 euclidian domain signal</figcaption></figure><p>As you can see again the first eigenvector is just plain image whose 2 direction frequency is just 0, then coming eigenvectors frequencies start to increase for both directions, till the last eigenvector has 10Hz for x-direction and 10Hz for the y-direction frequency which is the maximum frequency that 21x21 sampled 2D signal can carry. Again we created a random image and applied a certain low-pass filter on that image both two way and reported the results. Here is the code.</p><pre>% generate random iamge<br>s=randn(n,n);</pre><pre>% calculate centered frequcy of given image<br>F=fftshift(fft2(s));<br>[U V]=meshgrid(-fix(size(F,2)/2):fix(size(F,2)/2),-fix(size(F,1)/2):fix(size(F,1)/2));<br>UV=sqrt(U.^2+V.^2);<br>flt=exp(-1*UV);</pre><pre>% filter image in frequency domain<br>FD=F.*flt;</pre><pre>% transform the filtered image in to spatial domain<br>ot=ifft2(ifftshift(FD));</pre><pre>% create similar filter for graph filterin<br>fltg=zeros(n*n,1);<br>for i=1:n*n<br>  i1=GG(i,2);<br>  j1=GG(i,3);<br>  fltg(i,1)=flt((n+1)/2+j1,(n+1)/2+i1);<br>end </pre><pre>% apply filter on graph signal<br>sf=u*(fltg.*(u&#39;*s(:)));</pre><pre>% report results<br>figure;subplot(2,2,1);imagesc(s);axis image;title(&#39;Input image&#39;);subplot(2,2,2);imagesc(ot,[-0.05 0.2]);axis image;title(&#39;Classic Filtering Result&#39;);subplot(2,2,3);imagesc(reshape(sf,[n n ]),[-0.05 0.2]);axis image;title(&#39;Graph Signal Filtering Result&#39;);subplot(2,2,4);imagesc(reshape(sf,[n n ])-ot);axis image;title(&#39;Differences&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*mLuaAvfMiH6iG1idzQVlOg.png" /><figcaption>Fig9. Sample noise image and its filtered results</figcaption></figure><p>Again the results are similar. But we do not need to find the equivalent filter of both cases. We just need to define our filter as a function of eigenvalues.</p><p>Interesting things for a graph representation of 2D euclidian domain signal is the graph embedding. If in our W matrix definition, we get connected the leftmost column pixels to the rightmost column but cut the connection of the most up line pixels to the most bottom-line pixel. Here is the result of graph embedding and the filtered signal on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*aP0DqKK3OoYOxr6sV0LB9g.gif" /><figcaption>Fig10. The filtered signal representation of the embedded graph. Embedding was done by minimum 3 nonzero eigenvectors</figcaption></figure><h3>Graph Signal Filtering for Non-Euclidian Domain</h3><p>Now time to make something valuable. We have a connected, undirected any graph where it has 100 nodes and node’s predefined locations. The graph is a weighted graph so W matrix consists of 0 or positive real numbers. Here is loading predefined graph and eigenvector calculations.</p><pre>clear all</pre><pre>% load predefined W matrix for 100 nodes<br>load mydata</pre><pre>% calculate Laplacian Matrix<br>d = sum(W,2);<br>L = diag(d)-W;</pre><pre>% find eigenvector and eigenvalues of Laplacian<br>[u v]=eig(L);</pre><pre>% make eignevalue as vector<br>v=diag(v);</pre><pre>% get maximum eigenvalue<br>lmax=max(v);</pre><p>Now we need to define a filter. Let&#39;s do it again low pass filter by the following equation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*_fn-1GOhQz3jYm-elF8R9g.png" /></figure><p>This definition is definitely low pass filter. Since if the lower λ values the function’s output become high. (for 0 input it gives 1 output) but for higher λ values the output becomes low. So that function passes low frequency but suppresses high frequency. We want to apply that filter where the signal on the graph is all zero but just 1st node signal is 1. Here is the code for signal definition, filter definition, applying it on to the Graph signal and visualization of the results.</p><pre>% create signal where first node is 1 rest of them 0<br>s=zeros(size(W,1),1);<br>s(1)=1;</pre><pre>% define filter<br>flt =exp(-100*v/lmax);</pre><pre>% apply that filter on to graph signal<br>sf=u*(flt.*(u&#39;*s));</pre><pre>% visualize input and result<br>G=gsp_graph(W,coord);<br>figure;gsp_plot_signal(G,s);title(&#39;Input signal&#39;);<br>figure;gsp_plot_signal(G,sf);title(&#39;Filtered signal&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qVRJmxfjVxyIyFD_L4c74A.png" /><figcaption>Fig 11. The input signal on a predefined graph and its low-pass filtering result</figcaption></figure><p>The result seems extremely logical. Hence just the one node has high value but the rest of them is zero, we expected from low-pass filter to spread that value around and make the signal blur. The result is exactly what we expected.</p><h3><strong>Graph Embedding According to Node Distance</strong></h3><p>Another interesting feature of the Laplacian Matrix and its Eigenvectors is weighted Graph embedding as long as connected node distances are known, but there is no location information. In this case, we can use again minimum non-zero eigenvalues&#39; eigenvectors to estimate the positions of the nodes by the minimum square error solution. To demonstrate that feature first nodes over the circle created but the distance of the consecutive nodes is random. After we set the connected nodes weight as w(i,j)=w(j,i)=1/d(i,j) . Here are the codes.</p><pre>% create time series grid as graph<br>n=101;<br>W=zeros(n,n);<br>ang=linspace(0,2*pi,10*n);<br>p=randperm(length(ang));<br>p=sort(p(1:n));</pre><pre>p=[p p(1)];<br>for i=1:n+1<br>  loc(i,:)=[cos(ang(p(i))) sin(ang(p(i)))];<br>end</pre><pre>for i=1:n-1<br>  d=norm(loc(i,:)-loc(i+1,:));<br>  W(i,i+1)=(1/d);<br>  W(i+1,i)=(1/d);<br>end</pre><pre>d=norm(loc(1,:)-loc(n,:));<br>W(1,n)=(1/d);<br>W(n,1)=(1/d);<br>W=W/max(W(:));</pre><p>Then again Laplacian and eigenvector calculations.</p><pre>% calculate Laplacian Matrix<br>d = sum(W,2);<br>L = diag(d)-W;</pre><pre>% calculate basis<br>[u v]=eig(L);<br>v=diag(v);</pre><p>Signal creating and reporting code.</p><pre>% create arbitrary signal<br>s=1:n;<br>% show graph and signal<br>G=gsp_graph(W,loc(1:end-1,:));<br>figure;gsp_plot_signal(G,s)<br>title(&#39;original coordinates &#39;);</pre><pre>G=gsp_graph(W,u(:,2:3));<br>figure;gsp_plot_signal(G,s)<br>title(&#39;esimated coordinates&#39;);</pre><pre>du=diff(u(:,2:3));<br>dt=diff(loc(1:end-1,:));<br>figure;plot(diag(du*du&#39;),diag(dt*dt&#39;),&#39;r.&#39;)<br>xlabel(&#39;estimated &#39;);ylabel(&#39;actual &#39;);<br>title(&#39;consecutive node distance&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MHh7WCR0Dy06Z8c5aBgYbQ.png" /><figcaption>Fig12. Graph embedding by known connected node’s distance, Node color shows the number of the node</figcaption></figure><p>The result is not perfect but pretty good. Also, actual consecutive node distance and estimated node distances are almost linear. This result encourages us to use a weighted graph representation for euclidian signal where the sampling rate is variable.</p><h3><strong>What is Next?</strong></h3><p>So far the mechanism of graph signal processing is mostly figured out. That is the logic behind the Spectral Graph Neural Network which is one of the graph CNN methods. This method basically applies the filters on frequency domain instead of classical CNN usage as a spatial domain filter. The next step must be figuring out spatial filtering on graph signals.</p><h3><strong>Note</strong></h3><p>This research was funded by LITIS Lab at University of Rouen-Normandy. It is part of AGAC Project (Projet Régional RIN Recherche sur l’analyse de graphes et ses applications à la chemoinformatique).</p><p>You can find the demo code on my <a href="https://github.com/balcilar/Graph_Signal_Processing">GitHub </a>profile.</p><h3><strong>References</strong></h3><p>[1] Grassi, Francesco, et al. “A time-vertex signal processing framework.” <em>arXiv preprint arXiv:1705.02307</em> (2017).</p><p>[2] Marsden, Anne. “Eigenvalues of the Laplacian and Their Relationship to the Connectedness of a Graph.” <em>University of Chicago, REU</em> (2013).</p><p>[3] Roughgarden, Tim, and Gregory Valiant. “CS168: The Modern Algorithmic Toolbox Lectures# 11 and# 12: Spectral Graph Theory.” (2016).</p><p>[4] Perraudin, Nathanaël, et al. “GSPBOX: A toolbox for signal processing on graphs.” <em>arXiv preprint arXiv:1408.5781</em> (2014).</p><p>[5] Poignard, Camille, Tiago Pereira, and Jan Philipp Pade. “Spectra of Laplacian matrices of weighted graphs: structural genericity properties.” <em>SIAM Journal on Applied Mathematics</em>78.1 (2018): 372–394.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34674e699df8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make Sure Both Spatial and Frequency Domain DoG Filters are Same]]></title>
            <link>https://balcilar.medium.com/make-sure-both-spatial-and-frequency-domain-dog-filters-are-same-c3176b0ffe74?source=rss-4dff2fe6d340------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3176b0ffe74</guid>
            <category><![CDATA[difference-of-gaussian]]></category>
            <category><![CDATA[signal-processing]]></category>
            <category><![CDATA[image-filter]]></category>
            <category><![CDATA[matlab]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[M. Balcilar]]></dc:creator>
            <pubDate>Fri, 22 Feb 2019 21:37:58 GMT</pubDate>
            <atom:updated>2019-02-24T21:20:14.450Z</atom:updated>
            <content:encoded><![CDATA[<p>In my first writing on this platform, I am going to share a small but very detailed point of my recent research. Everybody knows that reimplement research paper is not a straight-forward task. As everybody, me too do not share every single detail of the implementation clues in the research paper. Sometimes I think it is not necessary to mention them because it is a very fundamental fact, but sometimes I just want readers to figure it out themselves.</p><p>The reason for writing that blog is because I have just figured out some details on implementing DoG filter which represents in one paper in the spatial domain [1] but another paper in the frequency domain [2]. Both researchers used that filter on the preprocessing step. I just wanted to reimplement both two papers. There is no question that both filters are the same but because of some missing details, it was not easy to show it at the beginning. Let’s have a look the formula of both filters. Here is the Difference of Gaussian (DoG) filter in the spatial domain [1].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*Mfezz3ul8ZWnr-5SyEXadQ.png" /></figure><p>Another paper represents the DoG in the frequency domain using almost the same notation [2].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*JGv6LUsHypvYmFDRCZIfgg.png" /></figure><p>Both papers use a series of DoG filer for j=1,2,3,…,P. But let’s simulate it just for j=1. So we will have one DoG filter on spatial domain and one DoG filter on the frequency domain. As it is well-known fact that DoG filter is the differences of two Gaussian low pass filter whose sigma’s are different. According to papers, the sigmas of Gaussian filters are qσj and σj respectively. Note q in the first equation and Q in second equations are the same. In the first equation, r refers to the distance of given (x,y) position’s to the center of the filter mask. Although in frequency domain filter ρ symbol was not mentioned but since it is for the 2D signals like images, it is quite clear that ρ refers a vector whose element is x-directional and y-directional frequency as u and v. So I assumed ρ=[u v].</p><p>Now hands on. Let’s write a code! Here is parameter definition</p><pre>% params of filter mentioned in the paper<br>sj=0.03;<br>q=2;</pre><p>To demonstrate the result I used gaussian noise as input image because this image has theoretically all frequencies. Here is the creating of image and calculation of its centered frequency map.</p><pre>% create random image for test n size of 401x401<br>I=randn(401,401);</pre><pre>% calculate centered frequcy of given image<br>F=fftshift(fft2(I));</pre><pre>figure;colormap(gray);imagesc(I);<br>figure;mesh(abs(F));</pre><p>The following figure shows the created input image and its frequency components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P38QwO6pwtfgys2I0xYGQg.png" /><figcaption>Fig1 Input image and its frequency profile</figcaption></figure><p>To create a DoG filter on the spatial domain, I wrote the following code lines. As you can see, instead of calculating the mask like in the given equation, I calculated them individually, normalized each of them individually, then subtracted them and finally I got the DoG filter in the spatial domain. Note that the normalization step was not mentioned then in the paper.</p><pre>% spatial domain filter (filter size nxn)<br>n=51;<br>[x y]=meshgrid(-(n-1)/2:(n-1)/2);<br>r=sqrt(x.^2+y.^2);</pre><pre>% calculate first part of filter<br>p1=2*pi*sj^2*(q*exp(-2*pi^2*q^2*sj^2*r.^2));</pre><pre>% then second part<br>p2=2*pi*sj^2*(exp(-2*pi^2*sj^2*r.^2));</pre><pre>% make sure both parts are normalized<br>p1=p1/sum(p1(:));<br>p2=p2/sum(p2(:));</pre><pre>% and set the spatial filter<br>sDoG=p1-p2;</pre><p>To create DoG filter on the frequency domain, the following codes were written. Since because of Nyquist theorem, the output of the FFT can represent the frequency components from 0th frequency till to the 0.5th relative frequency. Note 1 relative frequency means the frequency which is equal to the number of rows or cols. That is why I first created x and y-direction frequency grid map from -200 to 200 in case of our input image has 401 number of cols and rows. Then I got that mesh grid into the range of [-0.5 0.5].</p><pre>% frequency domain filter<br>% create meshgrid in the same resolution of given image</pre><pre>[U V]=meshgrid(-fix(size(F,2)/2):fix(size(F,2)/2),-fix(size(F,1)/2):fix(size(F,1)/2));</pre><pre>% note the last index must be -0.5 to 0.5 hence we must use normalized frequency<br>U=0.5*U/max(U(:));<br>V=0.5*V/max(V(:));</pre><pre>% calculate ro value<br>p=sqrt(U.^2+V.^2);<br>fDoG=exp(-0.5*(p/(q*sj)).^2)-exp(-0.5*(p/sj).^2);</pre><p>Now we have a spatial domain filter named sDoG and frequency domain filter named fDoG. So we are ready to apply that filters onto the given input image. Here is the filtering code. On the frequency domain, we first multiply the frequency profile of the image with the filter then take inverse FFT to get the image into the spatial domain to compare it. But in the spatial domain, filtering is just convolution of the mask on to the input image.</p><pre>% filter image in frequency domain<br>FD=F.*fDoG;</pre><pre>% transform the filtered image in to spatial domain<br>fI=ifft2(ifftshift(FD));</pre><pre>% filter image in spatial domain<br>oI=conv2(I,sDoG,&#39;same&#39;);</pre><p>Voilà !!. Now time to compare the results. Here is filtered images and their differences in code and figures.</p><pre>figure;<br>subplot(1,3,1);imagesc(fI);title(&#39;frequency filter result&#39;);<br>subplot(1,3,2);imagesc(oI);title(&#39;spatial filter result&#39;);<br>subplot(1,3,3);imagesc(fI-oI);title(&#39;differences&#39;);</pre><p>As you can see the images are quite similar. Just at the border, there is some artifact which is because of the padding problem for spatial filters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BRxRXtg0NCaYGs4Wif4O5Q.png" /><figcaption>Fig2. Filtered images and differences</figcaption></figure><p>In addition to reporting filtered images, we can report filtered images frequency profile as well. Within that representation, we can see how those filters effect on initially given image’s frequency profile.</p><pre>figure;<br>subplot(1,3,1);mesh(abs(fftshift(fft2(fI))));title(&#39;frequency of frequency filter result&#39;);<br>subplot(1,3,2);mesh(abs(fftshift(fft2(oI))));title(&#39;frequency spatial filter result&#39;);<br>subplot(1,3,3);imagesc(abs(fftshift(fft2(fI)))-abs(fftshift(fft2(oI))));title(&#39;differences of remaining freq&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xbMYuI6JyvspEpQZG2PdwA.png" /><figcaption>Fig3. Frequency profile of filtered images and differences</figcaption></figure><p>Again you can see both filters effects are almost the same. The differences are because of spatial filters mask size cannot be infinite in the application. So spatial filter is just an approximation of ideal frequency filter.</p><p>Last but not least, we can show the impulse response of both filters as well. Here is it.</p><pre>% impulse response of spatial filter<br>[H,f1,f2] = freqz2(sDoG);</pre><pre>figure;<br>subplot(1,3,1);mesh(imresize(fDoG,[size(H,1) size(H,2)]));<br>title(&#39;frequency filter&#39;&#39;s impulse response&#39;);<br>subplot(1,3,2);mesh(H);title(&#39;spatial filter&#39;&#39;s impulse response&#39;);<br>subplot(1,3,3);mesh(H-imresize(fDoG,[size(H,1) size(H,2)]));<br>title(&#39;impulse responses differences&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eiUYnj7dF0Xng7271ZkVRQ.png" /><figcaption>Fig4. Impuls response of filters and differences</figcaption></figure><p>Also, impulse response results encourage us to be sure that both filters are the same, but there are some minor differences because of finite spatial mask size which makes spatial filters as an approximation of ideal frequency filter.</p><p>You can find the full Matlab implementation on to my <a href="https://github.com/balcilar/Spatial_Frequency_DOG">GitHub </a>profile.</p><h3><strong>References</strong></h3><p>[1] Evaluation of the Impact of Tube Current Modulation on Lesion Detectability using Model Observers</p><p>[2] Human- and model-observer performance in ramp-spectrum noise: effects of regularization and object variability</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3176b0ffe74" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>