function [t,w,f,W]=myspectrum(in_file) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %This function is a spectrum analyzer %for tab delimited time-amplitude data %in an ASCII text file. % %S. Mandayam, Rowan University, ECOMMS S'03 % %Function Input: Filename %Function Outputs: %t=time sequence %w=time domain amplitude %f=frequency sequence %W=spectral amplitude % %The function assumes that you have %satisfied the sampling theorem; i.e. %fs >= 2 * f_max % %Usage: [t,w,f,W]=myspectrum('filename.txt'); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% close all; %loading input file; separating into time and amplitude in_signal=load(in_file); t=in_signal(:,1); w=in_signal(:,2); subplot(2,1,1); plot(t,w); xlabel('time in seconds'); ylabel('amplitude in volts'); title('Time domain signal'); %calculating parameters for finding spectrum N=length(t); T=t(N)-t(1); dt=T/N; fs=1/dt; n=0:N-1; %setting the frequency axis f=n/T; %calculating fft W=dt*fft(w); subplot(2,1,2); plot(f(1:round(N/2)),abs(W(1:round(N/2)))); xlabel(['frequency in Hz; Sampling Frequency = ', num2str(fs), ' Hz']); ylabel('amplitude in volts'); title('Amplitude Spectrum: My Low Cost Spectrum Analyzer');