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






Tidak ada komentar:

Posting Komentar