Skip to content

Commit

Permalink
Savitzky-Golay filter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloyeo committed Oct 21, 2020
1 parent 4dd95da commit 0f84a84
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 신호처리일반/Savitzky_Golay/no01_my_implementation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
clear; close all; clc;

M = 10; % 필터의 길이는 2M+1 = 21
N = 9; % 다항식의 차수는 9

% 테스트용 신호

load mtlb
t = (0:length(mtlb)-1)/Fs;


%% MATLAB으로 계수만 얻은 것
b = sgolay(N, 2*M+1);

sgolay_filter = b((size(b,1)+1)/2,:);

smtlb = conv(mtlb, sgolay_filter,'same');

%% MATLAB으로 직접 convolution까지 한 것
smtlb_MATLAB = sgolayfilt(mtlb, N, 2*M+1);

%% 직접 S-G filter의 계수까지도 계산
A = zeros(2*M+1, N+1);

n_range = -M:M; % 원래 논문에서 n
i_range = 0:N; % 원래 논문에서 i
for i = 1:size(A,1)
for j = 1:size(A,2)
A(i,j)= n_range(i)^i_range(j);
end
end

% matrix H = (A^TA)^{-1}*A^T

H = (A'*A)\A';

sgolay_filter_calculated = H(1,:);

my_smtlb_calculated = conv(mtlb, sgolay_filter_calculated,'same');

figure;
plot(t, mtlb);
axis([0.2 0.22 -3 2])
hold on;
plot(t, smtlb);
plot(t, my_smtlb_calculated);
plot(t, smtlb_MATLAB);

0 comments on commit 0f84a84

Please sign in to comment.