MATLAB Answers

FIltered signal not matching with the original signal.

조회 수: 5(최근 30일)
indigo fgo
indigo fgo 2021년 7월 13일
댓글: Mathieu NOE 2021년 7월 15일
Hello, im new to filter design, as you can probably tell. I have a signal from a wave guage measuring depth with time, The signal has very high low end frequencies so i am trying to apply a short low pass filter to smooth the signal, im not sure if it is my kernal or order choice but when i plot the original signal with the filtered signal, the filtered signal has lost magnitude. Its was originally intended to be a band stop filter so might not be consistant the whole way through. At this stage im completely stuck and any healp would greatly apreciated.
Thank you so much for your time,
clc
srate = 128;%Hz Sample Rate
nyquist = srate/2;
coff = [7];%Cut off or range
transw = .1;% randomly set
shape = [1 1 0 0];
frex = [1 coff(1)-coff(1)*transw coff(1) nyquist]/nyquist;
hz = linspace(0,srate,8321);%8321 is the number of samples in the data
% Loop for best matching order
order1 = (.2*srate/coff(1))/(srate/1000);%randomly set as I was trying different orders
order2 = (.3*srate/coff(1))/(srate/1000);
orders = round(linspace(order1,order2,2));%step count
fkernx = zeros(length(orders),8321);
figure(1)
for k = 1:length(orders);
fkern = firls(orders(k),frex,shape);
n(k) = length(fkern);
fkernx(k,:) = abs(fft(fkern,8321)).^2;
subplot(2,1,1)
hold on
plot((1:n(k))-n(k)/2,fkern+.1*k)
end
subplot(2,2,3)
hold on
plot(hz,fkernx)
plot(frex*nyquist,shape,'ro-')
xlim([0 64])
% % Loop for best matching transwidth
transwidth = linspace(.1,.4,4);
fkerxx = zeros(length(transwidth),8321);
orderl = round(order1);
figure(2)
for t = 1:length(transwidth);
frex2 = [1 coff(1)-coff(1)*transwidth(t) coff coff(2)+coff(2)*transwidth(t) nyquist]/nyquist;
fkern2 = firls(2,frex2,shape);
n(t) = length(fkern2);
fkernxx(t,:) = abs(fft(fkern2,8321).^2);
subplot(2,1,1)
hold on
plot((1:n(t))-n(t)/2,fkern2+0.1*t)
end
subplot(2,1,2)
hold on
plot(hz,fkernxx)
plot(frex2*nyquist,shape,'ro-')
%Final Filter Design
orderx = round((.3*srate/coff(1))-(srate/1000));%Use best matchig order here
transwidthx = 0.1;
frex3 = [1 coff(1)-coff(1)*transw coff nyquist]/nyquist;
filt28 = firls(orderx,frex3,shape);
%Apply filter design to signal (PDg1)
PDg1s = filtfilt(filt28,1,PDg1);
detfftp1 = detrend(PDg1s);
fftp1 = abs(fft(detfftp1));
%Frequency domain after filter
figure(3)
semilogy(hz, fftp1)
figure(4)
hold on
plot(PT5, PDg1,'r');
plot(PT5, PDg1s,'b')%plot smoothed data
legend('Original','Filtered')
xlabel('TIme (s)')
ylabel('Depth (m)')
title('Depth Against Time')

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 7월 13일
hello
I see that you are doing a detrend action on your data. This will remove the dc (mean) value of the signal , so no surprise that the output is shifted.
My suggestion :
T = csvread('ExampleData PDg1 and time.txt');
y = T(:,1);
Fs = 128;
dt = 1/Fs;
samples = length(y);
t = (0:samples-1)*dt;
%%% filter data %%%
N = 250;
y_LP = smoothdata(y, 'gaussian' , N); % low pass filtered data
%%% plot data %%%
figure(1)
plot(t,y,t,y_LP);legend('Raw','low pass filtered');
title(['Data samples at Fs = ' num2str(Fs) ' Hz / filtered with smoothdata' ]);
xlabel('Time (hours) ')
ylabel('Depth (m) ')
  댓글 수: 5
Mathieu NOE
Mathieu NOE 2021년 7월 15일
hello
if my answer has helped you, do you consider accepting it ?

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by