You are on page 1of 8

Audio Tachometer

FLOW CHART
CODE:
clc
clear
%audioread('Recording ().m4a')
[x,Fs] = audioread('Recording (85).m4a')
samples = [1,Fs];
clear x Fs
[x,Fs] = audioread('Recording (85).m4a',samples);
sound(x,Fs)

%
% plot waveform
t=(0:length(x)-1)/Fs; % times of sampling instants
subplot(2,1,1);
plot(t,x);
legend('Waveform');
xlabel('Time (s)');
ylabel('Amplitude');
%

ydft = fft(x);
freq = 0:Fs/length(x):Fs;
ydft = ydft(1:((length(x))+1));

% FFT Plot
subplot(2,1,2);
plot(freq,abs(ydft))
xlim([0 2*10^3])
legend('Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
findpeaks(abs(ydft(1:2000)),'MinPeakDistance',30,'MinPeakHeight',2);

[P,peaks]=findpeaks(abs(ydft(1:2000)),'MinPeakHeight',1);
peaks
PP=find(P>(0.1*max(P)))
peaks=peaks(PP)
f0=peaks(1)-1
speed=f0
rpm=speed*60/2
GUI:

function varargout = sns(varargin)


% SNS MATLAB code for sns.fig
% SNS, by itself, creates a new SNS or raises the existing
% singleton*.
%
% H = SNS returns the handle to a new SNS or the handle to
% the existing singleton*.
%
% SNS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SNS.M with the given input arguments.
%
% SNS('Property','Value',...) creates a new SNS or raises the
% existing singleton*. Starting from the left, property value
pairs are
% applied to the GUI before sns_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to sns_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only
one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help sns

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @sns_OpeningFcn, ...
'gui_OutputFcn', @sns_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before sns is made visible.


function sns_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to sns (see VARARGIN)

% Choose default command line output for sns


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes sns wait for user response (see UIRESUME)


% uiwait(handles.figure1);

function hrs_constant_Callback(hObject, eventdata, handles)


% hObject handle to hrs_constant (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of hrs_constant as text


% str2double(get(hObject,'String')) returns contents of
hrs_constant as a double

% --- Executes during object creation, after setting all properties.


function hrs_constant_CreateFcn(hObject, eventdata, handles)
% hObject handle to hrs_constant (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Outputs from this function are returned to the command line.
function varargout = sns_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in rpm1.


function rpm1_Callback(hObject, eventdata, handles)
% hObject handle to rpm1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc

[FileName,PathName] = uigetfile('*.*','Motor recording'); %Prompt user


to select a file
[y,Fs]=audioread(FileName)
samples = [1,Fs];
clear y Fs
[y,Fs] = audioread(FileName,samples);

%Load audio signal from selected file


sound(y,Fs) %PLay
selected motor sound

%Plot Sound signal


t=(0:length(y)-1)/Fs;
axes(handles.axes1);
plot(t,y);
title('Sound Signal')
legend('Motor Sound Waves');
xlabel('Time(s)');
ylabel('Amplitude');

%Fourier Transform of Sound signal

ydft = fft(y);
freq = 0:Fs/length(y):Fs;
ydft = ydft(1:((length(y))+1));

axes(handles.axes2);
plot(freq,abs(ydft))
xlim([0 2*10^3]);
title('FFT PLot');
legend('Frequency Spectrum');
xlabel('Frequency(Hz)');
ylabel('Magnitude(dB)');
findpeaks(abs(ydft(1:2000)),'MinPeakDistance',20,'MinPeakHeight',2)

[P,peaks]=findpeaks(abs(ydft(1:2000)),'MinPeakHeight',2)

PP=find(P>(0.1*max(P)))
peaks=peaks(PP)
%text(peaks,P,num2str(peaks))

f0=peaks(1)-1
speed=f0
constant=str2num(get(handles.hrs_constant,'string'));
rpm=speed*60/constant
motorspeed=num2str(rpm);
set(handles.ans,'string',motorspeed);
RESULT

You might also like