Search This Blog

Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

Friday, October 19, 2012

Variable Function Name in MATLAB

Suppose we want to call four different type of ODE solver, we can define a cell containing the 4 function handler as below:

functionName = {@ode45 @ode23 @ode113 @ode15s};

 and we can call in a cycle the desired function:
 for iFunction = 1 : size(functionName,2)  
   f = functionName{iFunction};  
   tol = 1e-4;  
   options = odeset ('RelTol',tol,'AbsTol',tol);  
   [t,y] = f('function_rhs', xspan, ic, options); % SOLVE ODEs 
 
   figure  
   plot(t,y)  
   title(func2str(f))  
 end 

Where function_rhs is the right side of the ODE we want to solve.
In this way we'll obtain 4 figures with the ODE solver name in the title.

Tuesday, May 3, 2011

2. Discrete Time Fourier Transform



Recall that if x(n) is absolutlely summable, that is, $\sum_{-\infty}^{+\infty} |x(n)| < \infty$ than its Discrete-Time Fourier Transform is given by:
\[
X(e^{j \omega})=\sum_{n=- \infty}^{+ \infty} x(n) \cdot e^{-j \cdot \omega \cdot n} \tag{3.1}
\]
This function transforms a discrete signal $x(n)$ into a complex-valued continuous function $X(e^{j \omega})$.

${\bf Matrix Implementation}$
If $x(n)$ is a finite duration sequence, we can use MATLAB to compute the Discrete Time Fourier Transform (DTFT) $X(e^{j\omega})$ numerically at any frequency $\omega$.

Let assume that the sequence x(n) has N samples between $n_1 \leq n \leq n_N$ we can define:
\[\omega_k =\frac{\pi}{M}k, \qquad k=0,1,....,M\]
which are (M+1) frequencies between $[0, \pi]$. Then (1) can be written as:

\[
X(e^{j\omega_k})=\sum_{r=1}^{N} x(n_r) \cdot e^{-j \cdot \omega_k \cdot n_r}  = \sum_{r=1}^{N} x(n_r) \cdot e^{-j \cdot \frac{\pi}{M} k \cdot n_r}, \qquad k=0,...,M \tag{3.2}
\]

Let's define the vector $\mathbf{X} = \begin{bmatrix} X(e^{j\omega_0}) \\ \vdots \\ X(e^{j\omega_M}) \end{bmatrix}$ where the k-th element $X(e^{j\omega_k})$ has the below expression:

\[X(e^{j\omega_k})= [ x(n_1) \cdot e^{-j \cdot \frac{\pi}{M}\cdot k \cdot n_1} + \ldots + x(n_N) \cdot  e^{-j \cdot \frac{\pi}{M}\cdot k \cdot n_N} ] \]

if we define the matrix $\mathbf{W}=\begin{bmatrix} W_0\\ \vdots \\W_M \end{bmatrix}$ where the k-th row $W_k$ has the below expression:

\[ W_k = \begin{bmatrix} e^{-j \cdot \frac{\pi}{M}\cdot k \cdot n_1}, \ldots, e^{-j \cdot \frac{\pi}{M}\cdot k \cdot n_N} \end{bmatrix} \]

and $\mathbf{x(n)} = \begin{bmatrix} x(n_1) \\ \vdots  \\ x(n_N) \end{bmatrix}$, we can write (3.2) as follow:

\[\mathbf{X} = \mathbf{W} \cdot \mathbf{x}\]

Let's consider the row vectors ${\bf k}$ and ${\bf n}$ that represent, respectively, the index of the frequency and for the time.
\[ \begin{matrix}
\mathbf{k}= \begin{bmatrix} 0, 1, \ldots, M\end{bmatrix}\\
\mathbf{n}= \begin{bmatrix} n_1, n_2, \ldots, n_N\end{bmatrix}
\end{matrix} \]
the product $\mathbf{k^T \cdot n}$ is equal to:
\[ \begin{bmatrix} 0 \\1 \\ \vdots \\M\end{bmatrix} \cdot \begin{bmatrix} n_1 & n_2 & \ldots & n_N\end{bmatrix} = \begin{bmatrix} 0 & 0 & \ldots & 0 \\ n_1 & n_2 & \ldots & n_N \\ \vdots & \vdots & \ddots & \vdots \\ M \cdot n_1 & M \cdot n_2 & \ldots & M \cdot n_N \end{bmatrix} \]
so we can rewrite $\mathbf{W}$

\[ \mathbf{W} = \begin{bmatrix} W_0 \\ W_1 \\ \vdots \\ W_M \end{bmatrix} = \begin{bmatrix} e^{-j \cdot \frac{\pi}{M}\cdot 0 \cdot n_1} \ldots e^{-j \cdot \frac{\pi}{M}\cdot 0 \cdot n_N} \\ \vdots \\ e^{-j \cdot \frac{\pi}{M}\cdot M \cdot n_1} \ldots e^{-j \cdot \frac{\pi}{M}\cdot M \cdot n_N} \end{bmatrix} =
 exp[-j \cdot \frac{\pi}{M} \cdot \mathbf{k^T \cdot n}] \]

finally the complete matrix form for DTFT is
\[\mathbf{X} = exp[-j \cdot \frac{\pi}{M} \cdot \mathbf{k^T \cdot n}] \cdot \mathbf{x} \tag{3.3} \]

Wednesday, March 16, 2011

Using of filter command in the convolution computation MATLAB

In the MATLAB exists the command filter that permits the resolution of difference equation numerically.
An important difference equation that express a link between input ad output in a LTI is
\[\sum_{k=0}^N a_ky(n-k) = \sum_{m=0}^M b_mx(n-m)\]
this equation can be solved using filter(b,a,x) where $b=[b_0 ... b_m],\; a=[a_0 ... a_k]$.
\[z(n) = conv(x,y) = x(n)*y(n) = \sum_{k=-\infty}^{+\infty} x(k)y(n-k)\]
Considering two sequence with finite length we can observe that:
\[filter(x,1,y)\Rightarrow 1\cdot z(n) =\sum_{m=0}^M x(m)y(n-m)\]
from below expression we can see that using filter function we can compute convolution of x and y but the result will have the dimension of x