2018/12/20

Matlab: Find the index/row and column of the maximum element of a matrix

To find out the index of the maximum element of a matrix in Matlab, use:

>> myMat = [1 2 3 4;2 3 4 5;5 6 2 1]

myMat =
     1     2     3     4
     2     3     4     5
     5     6     2     1

>> [row, col] = find(myMat == max(myMat(:)))

row =
     3


col =
     2

>> myMat(3,2)

ans =
     6

>> max(myMat(:))

ans =
     6

Reference

How do I find the indices of the maximum (or minimum) value of my matrix? (MathWorks)

2018/12/15

Matlab: Frequency response and pole-zero plot of a 1000-Hz Butterworth lowpass filter

The following Matlab code shows the magnitude and phase response and the Z-plane of a 1000-Hz Butterworth lowpass filter.

clc;clear;close all;

Fs = 8000;  %Sampling frequency
Fco = 1000; %Cutoff frequency
order = 4;
[b, a] = butter(order, Fco/(Fs/2), 'low');

zplane(b,a);title(['LPF Fco=',num2str(Fco),'Hz, Fs=',num2str(Fs),'Hz']);

w = 0:0.01:2*pi;

H = freqz(b, a, w);

mag = abs(H);
pha = angle(H);

figure
subplot(211);
plot(w,mag);title('Magnitude');xlabel('rad/s');
subplot(212)
plot(w,pha);title('Phase');xlabel('rad/s');

figure
w_Hz = w*Fs/(2*pi); %frequency in unit of Hz.
subplot(211);
plot(w_Hz,mag);title('Magnitude');xlabel('Hz');
subplot(212)
plot(w_Hz,pha);title('Phase');xlabel('Hz');

Result:

Pole-zero plot

Magnitude and phase responses with frequency in rad/s.

Magnitude and phase responses with frequency in Hz.

References:

How to plot the magnitude and phase of a given transfer function(z-domain)?
Pole-Zero plots of various filters
draw the Pole-Zero plot of the Z-transform

2018/11/29

Illustration of Fundamental Frequency, Harmonics, and Formats

How to show the differences between the fundamental frequencies F0, harmonics and formants F1, F2, F3 etc. of a speech signal on a spectral diagram?

The following presentation file gives a good illustration.

http://research.cs.tamu.edu/prism/lectures/sp/l7.pdf

See slide #3.

2018/11/06

Matlab: Pole-Zero plots of various filters

This page shows the comparisons of various Butterworth filter designs.

(1) Chang the sampling rate Fs = 4k, 8k, 16k. Fco = 200.

Fs = 2000 %4000, 8000, 16000
Fco = 200; %Cutoff frequency
order = 4;
[b, a] = butter(order, Fco/(Fs/2), 'low');

zplane(b,a);title(['LPF Fco=',num2str(Fco),'Hz, Fs=',num2str(Fs),'Hz']);

When the sampling frequency increases, the poles move towards to z = 1.





(2) Chang the cutoff frequency Fco = 200, 1k, 2k, 4k, 6k.

Fs = 16000
Fco = 200; %Cutoff frequency %200, 1000, 2000, 4000, 6000
order = 4;
[b, a] = butter(order, Fco/(Fs/2), 'low');

zplane(b,a);title(['LPF Fco=',num2str(Fco),'Hz, Fs=',num2str(Fs),'Hz']);

When the cutoff frequency increases, the poles move towards z = -1.



(3) Chang the order of the Butterworth filter. Order = 1, 2, 3, 4.

Fs = 16000
Fco = 1000;
order = 1; %1, 2, 3, 4
[b, a] = butter(order, Fco/(Fs/2), 'low');

zplane(b,a);title(['LPF Fco=',num2str(Fco),'Hz, Fs=',num2str(Fs),'Hz, Order = ',num2str(order)]);

The number of zeros = the order of the Butterworth filter.





(4) Chang the filter type: LPF, HPF, BPF, BSF.

Fs = 16000
Fco = 1000;
order = 4;
[b, a] = butter(order, Fco/(Fs/2), 'low'); %low, high

zplane(b,a);title(['LPF Fco=',num2str(Fco),'Hz, Fs=',num2str(Fs),'Hz']);

For the low pass filter, repeated zeros are at z=-1.
 For the high pass filter, repeated zeros are at z=1.
The band pass filter has repeated zeros at both z=1 and z=-1.

Fs = 16000
Fco1 = 1000; %3000
Fco2 = 2000; %4000
order = 4;
[b, a] = butter(order, [Fco1/(Fs/2),Fco2/(Fs/2)]);

zplane(b,a);title(['BPF Fco1=',num2str(Fco1),'Hz, Fco2=',num2str(Fco2),'Hz']);



The band stop filter (BSF) has repeated zeros on the unit circle. The zeros are surrounded by multiple poles.

Fs = 16000
Fco1 = 1000; %3000
Fco2 = 2000; %4000
order = 4;
[b, a] = butter(order, [Fco1/(Fs/2),Fco2/(Fs/2)],'stop');

zplane(b,a);title(['BSF Fco1=',num2str(Fco1),'Hz, Fco2=',num2str(Fco2),'Hz']);




Matlab: draw the Pole-Zero plot of the Z-transform

To plot the Pole-Zero diagram of the Z-transform on the Z-plane, write the transfer function in the form of b(z)/a(z) in terms of z-n:

z(z-1)/(z+1/2)(z-1/4) = (1 - z-1)/(1+1/2z-1)(1-1/4 z-1) =  (1 - z-1)/(1+1/4 z-1-1/8 z-2)

Call the zplane() function to draw the ploe-zero plot with coefficients of b(z)/a(z):

>> b=[1,-1];
>> a=[1,1/4,-1/8];
>> zplane(b,a);

Result:

The zeros are at 0 and 1 and the poles are at -1/2 and 1/4.


Reference:

zplane (MathWorks)

2018/10/06

Matlab: Sinc function

The code below shows how to use the sinc() function of Matlab's Signal Processing Toolbox:

x = (-6:1/100:6);
y1 = sinc(x);
plot(x,y1);
y2 = sinc(2*x);
plot(x,y2);
xlabel('x');ylabel('sinc'); title('Sinc Function')
legend('sinx(x)', 'sinc(2x)')

Result:

Note:
The sinc() function here is defined as

    sinc(x) = sin(πx)/πx (normalized sinc function)

References:

sinc - sinc function (MathWorks)
MATLAB sinc issue (StackOverflow)

2018/09/04

Matlab: Find the arguments of maxima/minima (argmax/argmin) of a vector

The argument of maximum means the index at which the vector/function values is maximized.

Example

>> a = [3 2 1 -2 7]

a =
     3     2     1    -2     7

>> [max_value, argmax] = max(a)

max_value =
     7

argmax =
     5

>> [min_value, argmin] = min(a)

min_value =
    -2

argmin =
     4

>> 

References:

Arg max (Wikipedia)
how do i find argmax? (MathWorks)