用matlab写一段滤波函数
时间: 2024-03-13 15:29:43 浏览: 22
function y = filter_func(x, fc, fs, order, type)
% x: 输入信号
% fc: 截止频率
% fs: 采样频率
% order: 滤波阶数
% type: 滤波类型,可选值为: 'lowpass', 'highpass', 'bandpass', 'bandstop'
% 计算归一化截止频率
wc = 2 * pi * fc / fs;
% 计算滤波系数
switch type
case 'lowpass'
[b, a] = butter(order, wc, 'low');
case 'highpass'
[b, a] = butter(order, wc, 'high');
case 'bandpass'
[b, a] = butter(order, wc, 'bandpass');
case 'bandstop'
[b, a] = butter(order, wc, 'stop');
otherwise
error('不支持的滤波类型');
end
% 使用滤波器对输入信号进行滤波
y = filter(b, a, x);
end
相关问题
用matlab写一段PCA滤波函数
function [filtered_signal] = PCA_filter(input_signal, num_components)
% input_signal: input signal to be filtered
% num_components: number of principal components to retain
% Center the input signal
mean_input = mean(input_signal);
centered_input = input_signal - mean_input;
% Compute the covariance matrix
cov_input = cov(centered_input);
% Compute the eigenvectors and eigenvalues of the covariance matrix
[eigenvectors, eigenvalues] = eig(cov_input);
% Sort the eigenvectors and eigenvalues in descending order of eigenvalue
[sorted_eigenvalues, index] = sort(diag(eigenvalues), 'descend');
sorted_eigenvectors = eigenvectors(:, index);
% Retain only the specified number of principal components
principal_components = sorted_eigenvectors(:, 1:num_components);
% Project the centered input signal onto the principal components
projected_signal = centered_input * principal_components;
% Reconstruct the filtered signal by projecting the projected signal back onto the original signal space
filtered_signal = projected_signal * principal_components' + mean_input;
end
用matlab写一段SPA滤波函数
function [y] = SPA_filter(x,fs,f0,df,Q)
%SPA_filter Summary of this function goes here
% This function implements a single-pole all-pass (SPA) filter.
% x: input signal
% fs: sampling rate
% f0: center frequency
% df: frequency deviation
% Q: quality factor
% Calculate the filter coefficients
w0 = 2*pi*f0/fs;
w1 = 2*pi*(f0+df)/fs;
w2 = 2*pi*(f0-df)/fs;
a0 = tan(w1/2)-tan(w2/2);
b0 = -cos(w0);
c0 = 1/tan(w1/2)+1/tan(w2/2);
d0 = 1-c0/2;
% Initialize the output signal
y = zeros(size(x));
% Apply the filter to the input signal
for n = 2:length(x)
y(n) = a0*(x(n)+x(n-1))-b0*y(n-1)+c0*x(n)+d0*y(n-1);
end
% Normalize the output signal
y = y/max(abs(y));
end