2018/03/29

Verilog Development Environment with Mac

To develop Verilog codes with a Mac, the following tools may be used:

Command line editor: Nano

Graphical UI Editor: TextWrangler (Available on App Store for Free)

Compiler: Icarus Verilog (Install with Homebrew with command: brew install icarus-verilog)

Waveform Viewer: GTKWave (Write $dumpfile command to output vcd file

References:

如何在Mac OS X上安裝Verilog環境 (Instructions to Icarus Verilog and GTKWave installation)
is there a good verilog editor for mac?

Verilog vs. VHDL (Study EECC)
Digital Logic Design - Truth Table and K-map (Study EECC)

2018/03/16

Digital Logic Design - Truth Table and K-map

To convert digital logic conditions in a truth table to a digital circuit, use the K-map.

truth table 真值表
Karnaugh map / K-map 卡諾圖
Boolean algebra / logic algebra 布林代數 / 邏輯代數

References

Karnaugh map (Wikipedia)
Truth table (Wikipedia)
Boolean algebra (Wikipedia)
HOW TO: Combinational logic: Truth Table → Karnaugh Map → Minimal Form → Gate Diagram (YouTube)

Verilog Development Environment with Mac (Study EECC)

2018/03/10

Matlab: Run Hello World files written in C/C++ with MEX command

To run C/C++ files from Matlab, use the MEX command.
Create the files in C or C++:

C (hello.c)

#include"mex.h"
#include<stdio.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    printf("Hello, World!\n");
}


C++ (helloCPP.cpp)

#include"mex.h"
#include <iostream>

using namespace std;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    cout << "Hello, World!\n";

}

The mexFunction in either of the C or C++ example above is similar to the main function in pure C or C++ programs. Type your main program here such as printf in C or cout in C++.

Execute the files with the MEX command in Matlab such as below:

C
mex hello.c
hello

C++
mex helloCPP.cpp
helloCPP

Results in the Matlab command window:


References

Matlab 教材:測試 MEX-file
C language with Mac: Hello World with Mac's Terminal
C++ with Mac: Hello World with Mac's Terminal

2018/02/23

Verilog vs. VHDL

Verilog -
1984年推出、1995年成為標準IEEE 1364-1995、較廣泛使用、接近程式語言

VHDL (VHSIC Hardware Description Language) -
1983開始、1987年成為標準IEEE 1076-1995、使用較Verilog少、學習時間較Verilog長、仍有其優點

參考資料
Verilog (Wikipedia)
VHDL (Wikipedia)
SystemVerilog (Wikipedia)
Verilog HDL和VHDL的比較

Verilog Development Environment with Mac (Study EECC)

2018/02/06

Matlab: Generate a pure tone with *.m file

To play a pure tone (sine wave) in Matlab, type the following code in a new file and save it as genTone.m:

function genTone(Fs, F, time)
t = 0:1/Fs:time;
y = sin(2*pi*F*t);
sound(y,Fs);
end

Now play sound in command

>> genTone(16000,100,0.5);
>> genTone(16000,1000,0.5);

The 3000Hz sound below will be wrong since it is greater than the 4000/2 = 2000 Hz Nyquist frequency.

>> genTone(4000,3000,0.5);

References:

how do i generate sound using MATLAB?
Chapter 2: Practical Audio Processing / tonegen.m (http://mcloughlin.eu/)

2018/01/26

Matlab: Divide a speech utterance into 3 bands


>> load mtlb %Load the 'Matlab' speech / dimention: 4001x1
>> sound(mtlb,Fs); %Play the original sound
>> len = length(mtlb); %Number of points
>> time = [1:len]/Fs; %x-axis time vector / dimention: 1x4001
>> subplot(5,1,1);
>> plot(time,mtlb);
>> title('Original Speech');

>> Fco1 = 1000/(Fs/2); %Cutoff freq. 1
>> Fco2 = 2000/(Fs/2); %Cutoff freq. 2

>> [Bhpf, Ahpf] = butter(4,Fco2,'high'); %design a HPF
>> [Bbpf, Abpf] = butter(4,[Fco1 Fco2],'bandpass'); %design a BPF
>> [Blpf, Alpf] = butter(4,Fco1,'low'); %design a LPF


>> y_hpf = filter(Bhpf, Ahpf, mtlb); %get time domain result

>> y_bpf = filter(Bbpf, Abpf, mtlb); %get time domain result
>> y_lpf = filter(Blpf, Alpf, mtlb); %get time domain result

>> subplot(5,1,2);
>> y_reconstruct = y_hpf + y_bpf + y_lpf;
>> plot(time,y_reconstruct);
>> title('Reconstructed Speech');
>> sound(y_reconstruct,Fs); %Play the reconstructed sound

>> subplot(5,1,3);
>> plot(time,y_hpf);
>> title('HPF Speech');
>> sound(y_hpf,Fs); %Play the HPF sound


>> subplot(5,1,4);
>> plot(time,y_bpf);
>> title('BPF Speech');
>> sound(y_bpf,Fs); %Play the BPF sound


>> subplot(5,1,5);
>> plot(time,y_lpf);
>> title('LPF Speech');
>> sound(y_lpf,Fs); %Play the LPF sound


Result:


2018/01/06

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

The code below shows how to implement a 1000Hz Butterworth low pass filter using the filter() function in time domain and freqz() function in the complex frequency domain.

>> load mtlb %Load the 'Matlab' speech / dimention: 4001x1
>> sound(mtlb,Fs); %Play the original sound
>> len = length(mtlb); %Number of points
>> time = [1:len]/Fs; %x-axis time vector / dimention: 1x4001
>> subplot(3,1,1);
>> plot(time,mtlb);
>> title('Original Speech');

>> [b, a] = butter(3,1000/(Fs/2),'low'); %design a Butterworth filter


Time domain

>> y1 = filter(b, a, mtlb); %get time domain result directly % dimention: 4001x1
>> sound(y1,Fs); %Play the sound processed by filter()
>> subplot(3,1,2);
>> plot(time,y1);
>> title('filter()');

Frequency domain (Method 1)
>> mtlb_FFT = fft(mtlb); %transformed input signal / dimention: 4001x1
>> H = freqz(b, a, len, 'whole'); %filter transfer function / dimention: 4001x1
>> Y2_FFT = mtlb_FFT .* H; %filtered result in freq. domain = multiplication of transformed speech signal and the filter transfer function in the freq. domain. % dimention: 4001x1
>> y2 = ifft(Y2_FFT); %Inverse FFT to time domain % dimention: 4001x1
>> sound(y2, Fs);
>> subplot(3,1,3);
>> plot(time,y2);
>> title('freqz() and transform');

Frequency domain (Method 2)
>> mtlb_FFT = fft(mtlb); %transformed input signal / dimention: 4001x1
>> H = freqz(b, a, len); %filter transfer function / dimention: 4001x1
>> Y2_FFT = mtlb_FFT .* H; %filtered result in freq. domain = multiplication of transformed speech signal and the filter transfer function in the freq. domain. % dimention: 4001x1
>> y2 = ifft(Y2_FFT); %Inverse FFT to time domain % dimention: 4001x1
>> sound(abs(y2), Fs);
>> subplot(3,1,3);
>> plot(time,y2);
>> title('freqz() and transform');

Result:


References:

Matlab: Low pass filtered Chirp sound - Study EECC
Filter Applications (濾波器應用) - Audio Signal Processing and Recognition (音訊處理與辨識)
MATLAB: filter in the frequency domain using FFT/IFFT with an IIR filter - StackOverflow