Showing posts with label Matlab Pemrosesan Sinyal. Show all posts
Showing posts with label Matlab Pemrosesan Sinyal. Show all posts

Sunday, December 28, 2014

Menghitung MSE dari .wav file di Matlab

MSE (mean squared error) adalah salah satu standar evaluasi obyektif (objective evaluation) pada dunia sains dan teknik, baik pada cabang ilmu fisika, kimia, biologi, informatika, elektronika dan bidang rekayasa yang lain. Misalkan kita mempunyai sebuah data atau sinyal (suara, gambar, dll) yang telah kita manipulasi (enhancement, separation, improvement) dan kita ingin membandingkan sinyal output (estimasi, enhanced, separated) dengan sinyal aslinya, maka kita membandingkan sinyal asli dan sinyal estimasi tersebut dengan teknik MSE dengan salah satu syarat: panjang sinyalnya sama.

Secara matematik, MSE dirumuskan sebagai berikut,


Sedangkan implementasi dalam matlab untuk file suara yang berekstensi .wav dapat dilihat pada kode di bawah. Cara menggunakan fungsi ini cukup mudah, yakni (dalam Matlab command window): msewav('fileinput.wav','fileoutput.wav').

Pastikan file fungsi ini beserta dua file wav yang akan dihitung MSE-nya ada dalam satu folder (direktori) dimana anda bekerja, atau gunakan perintah addpath untuk menambahkannya ke dalam direktori kerja anda.

msewav.m (Github )

Monday, November 07, 2011

Calculate Time Lag from Cross-correlation in Octave - Matlab

Calculation of time lag or time delay between two identical signal is very important in many areas, especially in system identification. By knowing time delay or time lag, we can analysis the signal such as subtract the output signal length according to the time lag. This problem usually appears in signal processing, control system, process, acoustic (determine time lag between sound sources and microphone) or other systems with input and output data.

In the other side, cross-corelation is useful tools. Using cross-correlation we can find the strongest point of correlation between two signal, and then shift the signal according to distance of strongest point to zero position. But the problem is the x-axis in cross-correlation not the time or sampled time (length), it is index. Let's solve by bringing the problem to computation methods such as GNU/Octave or Matlab.

Suppose, we have two signal in case of signal enhancement process. The first signal is true target signal, and the second is enhanced signal. We want to know, the time lag between true target signal and enhanced signal. The time lag can be resulted because of algorithm processing and others.

First, we read sound data in Octave or Matlab. We type,

[signal, fs]=wavread('signal.wav');

[enhance,fs]=wavread('enhance.wav');

And then, calculate the cross-correlation between signal and enhanced signal using the following command, 

Tuesday, October 18, 2011

Generating White Noise Sound on Octave / Matlab


The following white noise sound was generated with GNU Octave's random number generator rand(), which generates uniformly distributed random values in the interval [0,1). The wavwrite() function expects values in [-1.0, 1.0), so we multiply by 2 and shift down by 1. So to generate 10 seconds of noise sampled at 48 kHz:
white=rand(48000*10,1)*2-1;
I'm not sure about the inner workings of /dev/urandom, but I used the "reseed" command to feed it data from random.org, so this should be pretty random. To export this to a 16-bit, 48 kHz .wav file in our home directory, the command is:

Friday, October 14, 2011

FFT dari Sinyal Sinus di Matlab/Octave

Seharusnya, bila kita membuat sinyal sinusoidal dengan frekuensi 20 Hz, maka kalau kita fft-kan (fourrier transform-kan) sinyal tersebut memiliki frekuensi tunggal pada sumbu X=20. Bila tidak, atau plotnya menunjukkan spektrum yang tidak beraturan, maka gambar dan teknik yang kita gunakan tersebut salah.
Sinyal Sin 20 Hz dan FFT -nya
Cek dan run listing code matlab di bawah untuk menghasilkan FFT dari sinyal sinus yang match dengan frekuensinya.
% Sampling frequency 
Fs = 1024; 
% Time vector of 1 second 
t = 0:1/Fs:1; 
% Create a sine wave of 20 Hz.
x = sin(2*pi*t*20);
% Use next highest power of 2 greater than or equal to length(x) to calculate FFT.
nfft= 2^(nextpow2(length(x)));

% Take fft, padding with zeros so that length(fftx) is equal to nfft
fftx = fft(x,nfft);

% Calculate the numberof unique points
NumUniquePts = ceil((nfft+1)/2);

% FFT is symmetric, throw away second half
fftx = fftx(1:NumUniquePts);

% Take the magnitude of fft of x and scale the fft so that it is not a function of the length of x
mx = abs(fftx)/length(x);

% Take the square of the magnitude of fft of x.
mx = mx.^2;

% Since we dropped half the FFT, we multiply mx by 2 to keep the same energy.
% The DC component and Nyquist component, if it exists, are unique and should not be multiplied by 2.
if rem(nfft, 2) % odd nfft excludes Nyquist point
  mx(2:end) = mx(2:end)*2;
else
  mx(2:end -1) = mx(2:end -1)*2;
end

% This is an evenly spaced frequency vector with NumUniquePts points.
f = (0:NumUniquePts-1)*Fs/nfft;

% Generate the plot, title and labels.
subplot(211); plot(x);
title('Waveform of a 20Hz Sine Wave');
xlabel('Time');
ylabel('Amplitude');
subplot(212); plot(f,mx);
title('Power Spectrum of a 20Hz Sine Wave');
xlabel('Frequency (Hz)');
ylabel('Power');
Source: Mathwork

Tuesday, October 11, 2011

Menyimpan data .wav/.mat dalam format ASCII di Matlab / Octave

ASCII - American Standard Code for Information Interchange merupakan format standar data yang berupa text yang akan memudahkan kita untuk memprosesnya dalam program dan OS apapun. Nah, bagaimana menyimpan data kita dalam format ASCII ? Caranya adalah dengan menyimpan anda dalam workspace Matlab atau Octave, kemudian simpan sebagai file format ASCII dengan perintah berikut ini. (Mungkin juga berlaku untuk selain data .wav dan .mat, yakni data berekstensi lain .xml dll, sekali lagi load dulu ke workspace-nya matlab/Octave).

Dalam Matlab,
x=wavread('nama_file.wav');                      % load file wav anda

save namafiletxt x -ASCII


jika data anda berupa .mat

load data

save namafiletxt x -ASCII                           % load file mat


Dalam Octave, ganti -ASCII menjadi -ascii, sehingga menjadi sebagai berikut: