2020/12/03

Matlab: Vector normalization to a new peak or RMS value

The following example shows how to normalize a vector to a new peak or RMS (root-mean-square) value. The rms function from the digital signal processing toolbox is used. For details about the calculations for RMS, see a previous article.

RMS = √(∑xi2/n)

Vector a:

>> a = [1 3 10 2 5 7 8 17 14]

a =

     1     3    10     2     5     7     8    17    14

Normalize to a new peak:

>> target_peak = 15

target_peak =

    15

>> a_newpeak = a*target_peak/max(a)

a_newpeak =

    0.8824    2.6471    8.8235    1.7647    4.4118    6.1765    7.0588   15.0000   12.3529

Normalize to a new RMS value:

>> target_rms = 10

target_rms =

    10

>> a_newrms = a*target_rms/rms(a)

a_newrms =

    1.1051    3.3152   11.0506    2.2101    5.5253    7.7354    8.8405   18.7861   15.4709

Check the RMS value:

>> rms(a_newrms)

ans =

    10

Matlab: Min, Max, Mean, Square, Mean-Square, RMS

The following example shows how to get the minimum, maximum, average/mean and RMS (root-mean-square) values from a vector.

RMS = √(∑xi2/n)

>> a = [1 3 10 2 5 7 8 17 14]

a =

     1     3    10     2     5     7     8    17    14

>> b = min(a)

b =

     1

>> c = max(a)

c =

    17

>> d = mean(a)

d =

    7.4444

>> e = a.^2

e =

     1     9   100     4    25    49    64   289   196

>> f = mean(e)

f =


   81.8889

>> g = sqrt(f)

g =


    9.0492

where

b = minimum
c = maximum
d = mean/average
e = square
f = mean-square
g = root-mean-square (RMS)

The RMS value can also be worked out in a straight-forward command:

>> a_rms = sqrt(sum(a.^2)/length(a))

a_rms =

    9.0492

Or use rms in the digital signal processing toolbox:

>> a_rms_toolbox = rms(a)

a_rms_toolbox =

    9.0492

Reference:

Root mean square (Wikipedia)/平方平均數(維基百科)

2020/10/06

Matlab: Spectrogram of white noise processed by a 500-Hz Butterworth lowpass filter

Matlab code:

clc;clear;close all;

 

Fs = 16000;  %Sampling frequency

Fco = 500;   %Cutoff frequency

order = 4;

[b, a] = butter(order, Fco/(Fs/2), 'low');

 

%Generate white noise

x=2*rand(16000,1)-1;

 

y = filter(b, a, x); %get time domain result

 

spectrogram(x,[],[],[],Fs,'yaxis');colormap hot;title('x');figure

spectrogram(y,[],[],[],Fs,'yaxis');colormap hot;title('y');

Result:


References:

Divide a speech utterance into 3 bands

Apply a LPF in the time and frequency using filter() and freqz()

2020/09/01

Deep Learning @ Mobile Devices 在行動裝置上實現深度學習的幾種方法

 Some ways to implement deep learning on mobile devices:

1. Device - Inference Only 僅推論

Pre-trained models by cloud data centers are loaded into device. The model can be either fixed or can be updated later via network or product services.

訓練好的模型放在行動裝置上,模型除了是固定預載的,也可經網路下載或透過維修服務人員更新。

2. Device - Inference and Collect Data for Distributed Training 有回報資訊

The device collects data and transmit them for cloud data centers to train the model, which is then downloaded to update the device.

行動裝置可回報上傳適合更新模型的資訊

3. Federated Learning 聯合學習

Download the model to device and train the model with local data. The results of training and inference are uploaded to the cloud for updating the model.

在行動裝置上同時做訓練及推論/推斷/推理,再將結果上傳雲端,然後還可再下載新的模型至裝置

References:

Wang, J., Cao, B., Yu, P., Sun, L., Bao, W., & Zhu, X. (2018, July). Deep learning towards mobile applications. In 2018 IEEE 38th International Conference on Distributed Computing Systems (ICDCS) (pp. 1385-1393). IEEE.

行動裝置上的AI:使用TensorFlow on iOS Android及樹莓派,王眾磊、陳海波, 深智數位, p.3-1~3-3

McMahan, B., Moore, E., Ramage, D., Hampson, S., & Arcas, B. A. (2017, April). Communication-efficient learning of deep networks from decentralized data. In Artificial Intelligence and Statistics (pp. 1273-1282). PMLR.

分布式機器學習時代即將來臨?谷歌推出「Federated Learning」

2020/07/04

Compare the if condition between Matlab and Python

When coding with multiple programming languages, sometimes it is confusing to choose the right syntax for each language. Here is an example for the if condition in Matlab and Python:

Matlab

a = 1;
b = 2;

if a == 1
   disp('Hello');
else
   disp('Hi');
end
if b ~= 1
   disp('World'); 
end

Result:

Hello
World
>> 

Python

a = 1
b = 2

if a == 1:
   print('Hello')
else:
   print('Hi')

if b != 1:
   print('World')

Result:

Hello
World

2020/06/25

STOI Code Test for Matlab and Python

(這篇文章主要是在測試Matlab和Python兩個版本程式所計算出的STOI值是否有差異)
STOI (Short-time objective intelligibility) is a prediction metric useful to replace previous objective prediction measures.

I tried to check if the Matlab and Python versions of STOI codes work equally with the identical output scores.

The Matlab code is available from Cees Taal's web site.

The python version is available from Pariente Manuel's GitHub.

The same processed wav file and reference wav file are tested using Matlab and Python.

The Python version needs a few configurations.

Create minicoda environments
conda create -n py38_STOI python=3.8 pytorch
conda activate py38_STOI

Install sound packages
pip install SoundFile
conda install -c conda-forge librosa

Run the python file.
python STOI_test.py

where the STOI_test.py is modified from Pariente Manuel's GitHub:

import soundfile as sf
from pystoi import stoi #or use from pystoi.stoi import stoi

clean, fs = sf.read('my_clean.wav')
denoised, fs = sf.read('my_processed.wav')

# Clean and den should have the same length, and be 1D
d = stoi(clean, denoised, fs, extended=False)
print(d)

And the result is


0.8157936197843473


The result of Matlab version is

>> d = stoi(y_my_clean, y_my_processed, fs)
d =
    0.8158

The results are similar, but there is still a tiny difference when showing more decimal points using digits() and vpa() commands.

>> digits(16)
>> vpa(d)
ans =
0.8157974711433416

The difference between Matlab and Python versions is small than 0.00001 and hence can be ignored.

References:

Cees Taal's web site
Pariente Manuel's GitHub

Matlab: Show more decimal points for a variable

To show more digits with Matlab, use digits() and vpa()

>> a = 1/3

a =

    0.3333

>> digits(6)
>> vpa(a)

ans =

0.333333

>> digits(10)
>> vpa(a)

ans =

0.3333333333

Reference:

digits (MathWorks)

2020/06/13

Matlab: Log function with base 10

For the logarithmic function with base 10, simply use log10:

>> log10(100)

ans =

     2

>> 10*log10(0.1)

ans =

   -10

>> 20*log10(1000)

ans =

    60

>>

2020/05/14

隱藏式馬可夫模型(Hidden Markov Model)

隱藏式馬可夫模型(Hidden Markov Model, HMM)是語音辨識(Automatic Speech Recognition, ASR)常用到的技術。

Andrey Andreyevich Markov (1856-1922) 安德雷·安德耶維齊·馬可夫,俄國數學家
馬可夫模型(Markov Model)

HMM 的「白話文」介紹,有很好的比喻,有對於ASR、生物資訊學(bioinformatics)、預測股價等應用的簡介。在有限的資訊下,亦即無法取得的資訊是隱藏的,此時可使用HMM,以可取得、看得到的資訊,去判斷、預測在機率上具有相關可能性的隱藏、看不到的狀態。

References

隱藏式馬可夫模型(Wikipedia)
語音辨識(Wikipedia)

2020/05/08

Deep Learning Textbook by Ian Goodfellow, Yoshua Bengio and Aaron Courville

A good online textbook to start with deep learning, is available here:

https://www.deeplearningbook.org/

Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep learning. MIT press.

Authors:

Ian Goodfellow - Apple. Previous: Google Brain, OpenAI
Yoshua Bengio - Université de Montréal
Aaron Courville - Université de Montréal

Video introduction to deep learning:

Learn Deep Learning in 6 Weeks (YouTube video by Siraj Raval)

2020/04/29

LaTex - Citation for web site in IEEEtran format 使用IEEE格式引用網址

1. Edit the bib file as:

@misc{studyswift_key,
Author = {E Huang},
Note = {[Online]. Available: \url{https://studyswift.blogspot.com/}, Accessed on: Apr. 29, 2020},
Title = {Study {Swift}}}

2. Add this to the tex file:

\usepackage{url}

3. Cite the website:

\cite{studyswift_key}

3. Typeset: LaTex -> BibTex -> LaTex -> LaTex

Result:

Reference:

LaTeX的BibTeX引用网页的办法

2020/04/23

LaTex - *.bib file - Preserve Capital Letters 保留大寫字母

LaTex 的bib檔中的英文大寫預設上是會自動轉換為小寫英文字母

若要保留大寫,在bib檔中的字母前後加上 { } 即可

原始參考文獻 (Before)

修改bib檔 (Modify *.bib file)


重新編譯後的參考文獻 (After)

參考資料

BibTeX loses capitals when creating .bbl file

2020/04/06

How to draw figures with mathematical font for LaTeX

How to create figures for LaTeX documents

1. Create figure with PowerPoint
2. Save as PDF file
3. Open the PDF file with Inkscape and save as EPS file.
4. Use LaTex to include the EPS file

How to include math labels with consistent font with LaTeX

1. Install Latin Modern Math font
2. Reboot the computer and select the font in PowerPoint.

3. Proceed the above steps and save the file as EPS.

Reference:
https://tex.stackexchange.com/questions/22454/what-is-the-name-of-the-default-font-in-math-mode