(A)FFT AND HILBERT

%(A)FFT AND HILBERT
function [Hilb, Env] = envelope(D8,Freq)
%Read in Data from Text file
fileID = fopen('D8.001');
signal = cell2mat(textscan(fileID, '%f32'));
fclose(fileID);
%End of Data Read
clc;
close all;
%Normal FFT
Fs=256000;
y=signal;
N=256000;
T=N/Fs;
sig_f=abs(fft(y(1:N)',N));
sig_n=sig_f/(norm(sig_f));
freq_s=(0:N-1)/T;
clc;
close all;
%Envelope Detection using Hilbert Transform and Fast Fourier Transform
analy = hilbert(signal); %Hilbert transform on imported data
Hilb = abs(analy);
N = length(signal)
Freq=256000;
T = N/Freq;
sig_f=abs(fft(Hilb(1:N)',N));
Env=sig_f/(norm(sig_f));
freq_s=(0:N-1)/T;
figure(gcf); clf
subplot(211);plot(freq_s(2:250),sig_n(2:250));title('FFT of Original
Signal');legend('Fast Fourier
Transform');xlabel('Time(s)');ylabel('Magnitude')
grid on
subplot(212);plot(freq_s(2:250),Env(2:250));title('Envelope Detection :
Hilbert Transform');legend('Hilbert
Transform');xlabel('Frequency(Hz)');ylabel('Signal Amplitude')
grid on
end
%(B)Low Pass Filter
function [Lpf,Env] = envelop(D8,Freq)
%Read in Data from Text file
fileID = fopen('D8.001');
signal = cell2mat(textscan(fileID, '%f32'));
fclose(fileID);
%End of Data Read
%Close all other windows and clear command window
clc;
close all;
Freq=256000
%Envelope Detection based on Low pass filter and then FFT
[a,b]=butter(2,0.1);%butterworth Filter of 2 poles and Wn=0.1
%sig_abs=abs(signal); % Can be used instead of squaring, then filtering and
%then taking square root
sig_sq=2*signal.*signal;% squaring for rectifing
%gain of 2 for maintianing the same energy in the output
y_sq = filter(a,b,sig_sq); %applying LPF
y=sqrt(y_sq);%taking Square root
%advantages of taking square and then Square root rather than abs, brings
%out some hidden information more efficiently
figure();
N=length(signal);
T=N/Freq;
sig_f=abs(fft(y(1:N)',N));
sig_n=sig_f/(norm(sig_f));
freq_s=(0:N-1)/T;
plot(freq_s(2:250),sig_n(2:250));title('Envelope Detection: LPF Method');
grid
end