Minggu, 21 Juli 2019

KMEANS EXAMPLE


clc
clear all
close all
%% Generate Points
Sigma = [0.5 0.05; 0.05 0.5];
f1    = mvnrnd([0.5 0]  ,Sigma,100);
f2    = mvnrnd([0.5 0.5],Sigma,100);
f3    = mvnrnd([0.5 1]  ,Sigma,100);
f4    = mvnrnd([0.5 1.5],Sigma,100);
F     = [f1;f2;f3;f4];
%% K-means
K     = 8;                                            % Cluster Numbers
KMI   = 40;                                           % K-means Iteration
CENTS = F( ceil(rand(K,1)*size(F,1)) ,:);             % Cluster Centers
DAL   = zeros(size(F,1),K+2);                         % Distances and Labels
CV    = '+r+b+c+m+k+yorobocomokoysrsbscsmsksy';       % Color Vector
for n = 1:KMI
       
   for i = 1:size(F,1)
      for j = 1:K 
        DAL(i,j) = norm(F(i,:) - CENTS(j,:));     
      end
      [Distance CN] = min(DAL(i,1:K));                % 1:K are Distance from Cluster Centers 1:K
      DAL(i,K+1) = CN;                                % K+1 is Cluster Label
      DAL(i,K+2) = Distance;                          % K+2 is Minimum Distance
   end
   for i = 1:K
      A = (DAL(:,K+1) == i);                          % Cluster K Points
      CENTS(i,:) = mean(F(A,:));                      % New Cluster Centers
      if sum(isnan(CENTS(:))) ~= 0                    % If CENTS(i,:) Is Nan Then Replace It With Random Point
         NC = find(isnan(CENTS(:,1)) == 1);           % Find Nan Centers
         for Ind = 1:size(NC,1)
         CENTS(NC(Ind),:) = F(randi(size(F,1)),:);
         end
      end
   end
 
%% Plot 
clf
figure(1)
hold on
 for i = 1:K
PT = F(DAL(:,K+1) == i,:);                            % Find points of each cluster   
plot(PT(:,1),PT(:,2),CV(2*i-1:2*i),'LineWidth',2);    % Plot points with determined color and shape
plot(CENTS(:,1),CENTS(:,2),'*k','LineWidth',7);       % Plot cluster centers
 end
hold off
grid on
pause(0.1)
end

Rabu, 17 Juli 2019

FFT tes WAV

[y, fs] = audioread('Alarm08.wav');
soundsc(y, fs);
subplot(2, 1, 1);
plot(y, 'b-');
grid on;
drawnow;
message = sprintf('Do you want to Set to FFT mode ?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
  % User said No, so exit.
  return;
end

lengthOfData = length(y);
nextPowerOfTwo = 2 ^ nextpow2(lengthOfData); % next closest power of 2 to the length

plotScaleFactor = 4;
plotRange = nextPowerOfTwo / 2; % Plot is symmetric about n/2
plotRange = floor(plotRange / plotScaleFactor);

yDFT = fft(y, nextPowerOfTwo); % Discrete Fourier Transform of data

h = yDFT(1:plotRange);
abs_h = abs(h);

freqRange = (0:nextPowerOfTwo-1) * (fs / nextPowerOfTwo);  % Frequency range
gfreq = freqRange(1:plotRange);  % Only plotting upto n/2 (as other half is the mirror image)

subplot(2, 1, 2);
plot(abs_h, 'b-');
grid on;
drawnow;
soundsc(abs_h, gfreq);







UJI AUDIO REALTIME

d = daq.getDevices
% index   Vendor    Device ID                            Description                         
% ----- ----------- --------- -----------------------------------------------------------------
% 1     directsound Audio0    DirectSound Primary Sound Capture Driver
% 2     directsound Audio1    DirectSound Microphone (High Definition Audio Device)
% 3     directsound Audio2    DirectSound HP 4120 Microphone (2- HP 4120)
% 4     directsound Audio3    DirectSound Microphone (Plantronics .Audio 400 DSP)
% 5     directsound Audio4    DirectSound Digital Audio (S/PDIF) (High Definition Audio Device)
% 6     directsound Audio5    DirectSound Primary Sound Driver
% 7     directsound Audio6    DirectSound Speakers (Plantronics .Audio 400 DSP)
% 8     directsound Audio7    DirectSound HP 4120 (2- HP 4120)
% 9     directsound Audio8    DirectSound Speakers (High Definition Audio Device):1
% 10    directsound Audio9    DirectSound Speakers (High Definition Audio Device):2

%misalkan di laptop kita terdeteksi no 2:  2     directsound Audio1    DirectSound Microphone (High Definition Audio Device)

dev = d(2)
s = daq.createSession('directsound');
addAudioInputChannel(s, dev.ID, 1:2);
s.IsContinuous = true

load chirp.mat;
sound(y, Fs);

hf = figure;
hp = plot(zeros(1000,1));
T = title('Discrete FFT Plot');
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
grid on;
continuous_fft(y, Fs, hf)


%%REALIMR CONTINUOI
plotFFT = @(src, event) continuous_fft(event.Data, src.Rate, hp);
hl = addlistener(s, 'DataAvailable', plotFFT);


%start
startBackground(s);
figure(hf);


%stop
stop(s);
s.IsContinuous = false;
delete(hl);



%%%%%%%%%%%%%%%%%
function continuous_fft(data, Fs, plotHandle)

lengthOfData = length(data);
nextPowerOfTwo = 2 ^ nextpow2(lengthOfData); % next closest power of 2 to the length

plotScaleFactor = 4;
plotRange = nextPowerOfTwo / 2; % Plot is symmetric about n/2
plotRange = floor(plotRange / plotScaleFactor);

yDFT = fft(data, nextPowerOfTwo); % Discrete Fourier Transform of data

h = yDFT(1:plotRange);
abs_h = abs(h);

freqRange = (0:nextPowerOfTwo-1) * (Fs / nextPowerOfTwo);  % Frequency range
gfreq = freqRange(1:plotRange);  % Only plotting upto n/2 (as other half is the mirror image)

% whos abs_h
% whos gfreq

set(plotHandle, 'ydata', abs_h, 'xdata', gfreq); % Updating the plot
drawnow; % Update the plot

end