2018/06/16

Matlab: Plot discrete sequence data with STEM() 繪製離散序列資料

To plot discrete sequence data in Matlab, use the stem() function:

Example:
For y = e-x + 1 with x between -2 and 2, plot the continuous y curve and discrete stem diagram.

x = -2:0.1:2;
y = exp(-x)+1;
subplot(211);
plot(x,y);
xlabel('x');ylabel('y');title('y = exp(-x)+1');
subplot(212);
stem(x,y);

xlabel('x');ylabel('y');title('discrete x');

Result:



References:

stem - plot discrete sequence data (MathWorks)
Matlab: Unit Impulse δ[n] 單位脈衝函數

2018/06/15

Matlab: Resample 重新取樣

To change the sample rate of an audio signal with Matlab, use the resample function:

load mtlb;
sound(mtlb,Fs);
pause(1); %Wait until the sound is finished
Fs_new = 16e3; %change the sample rate to 16000
[P,Q] = rat(Fs_new/Fs);

y = resample(mtlb,P,Q);

sound(y,Fs_new);

Reference

Changing Signal Sample Rate (MathWorks)

2018/06/11

Floating-Point

IEEE 754-1985, 754-2008
IEEE single-precision standard (binary32) - 32 bits
IEEE double-precision standard (binary64) - 64 bits

single-recision (binary32) -
1 bit - sign (S)
8 bits - exponention (E)
23 bits - mantissa (M)

Number = (-1)S×1.M×E-127

Reference:

Tutorial: Floating-Point Binary
Floating point ALU using VHDL implemented on FPGA
Single-precision floating-point format (Wikipedia)
Double-precision floating-point format (Wikipedia)

Two's complement

For representing +ve and -ve numbers.

The most significant bit (leftmost) bit:
0 => 0 or positive
1 => negative

e.g. 01110000 => +ve
e.g. 11110000 => -ve

If the left most bit = 0,

01110000 = 2^6+2^5+2^4 = 64+32+16 = 112

If the left most bit = 1, invert every remaining bit and add 1.

11110000 => 0001111 + 1 => 0010000 => -16

Reference:

Two's complement (Wikipedia)

2018/06/07

Matlab: Real and imaginary parts of complex number and multiplication with imaginary unit j

For a complex number x = 1 + 2i or x = 1 + 2j

Matlab commands for real part - real(x)
Matlab commands for imaginary part - imag(x)

Example:

>> x = 1 + 2j

x =

   1.0000 + 2.0000i

>> real(x)

ans =

     1

>> imag(x)

ans =

     2

>>

To multiply x with an imaginary unit i or -i, simply swap the real part with the imaginary part and add a negative sign.

Example:


x = 1 + 2j
mult_pos_j(x)
mult_neg_j(x)

function result = mult_pos_j(x)
    % x = a + bj
    % x(j) = aj - b = -b + aj
    result = -imag(x) + real(x)*j;
end

function result = mult_neg_j(x)
    % x = a + bj
    % x(-j) = -aj + b = b - aj
    result = imag(x) - real(x)*j;
end

Result:

x =

   1.0000 + 2.0000i


ans =

  -2.0000 + 1.0000i


ans =

   2.0000 - 1.0000i

Related Information:

Complex numbers - simple calculations (StudySwift - iOS)

2018/05/31

Identity Matrix 單位矩陣

Identity Matrix / Unit Matrix 單位矩陣/恆等矩陣

Example:

I =
[ 1 0 ]
[ 0 1 ]

I =
[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]

I =
[ 1 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]

Matlab Example:

>> I = eye(4)

I =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

>>

For an NxN identity matrix I and a NxN matrix A,

AI = IA = A

For an NxN identity matrix I and a Nx1 matrix B or a 1xN matrix C,

IB = B
CI = C

Reference:

Identity Matrix (Wikipedia/維基百科)

2018/05/05

Matlab: cross-correlation and autocorrelation 互相關與自相關

cross-correlation 互相關
autocorrelation 自相關

For autocorrelation function in Matlab:
autocorr - Econometrics Toolbox - for statistics. 😳
xcorr - Signal Processing Toolbox - for engineers!! 😄

To use xcorr:
xcorr(x, y) => cross-correlation of discrete signals x and y.
xcorr(x) => autocorrelation of x. Special condition of cross-correlation since auto correlation is the cross-correlation of signal x and shifted version of itself.

Autocorrelation is a special case of cross-correlation.

Autocorrelation may be used for pitch (fundamental frequency, F0) detection.


References:

Cross-correlation (Wikipedia)
Autocorrelation (Wikipedia)
Pitch detection algorithm (Wikipedia)
D. Gerhard. Pitch Extraction and Fundamental Frequency: History and Current Techniques, technical report, Dept. of Computer Science, University of Regina, 2003.