matlab脑电信号频域分析butterworth滤波器代码
时间: 2023-04-04 18:01:49 浏览: 222
以下是 Matlab 脑电信号频域分析 butterworth 滤波器代码:
% 导入数据
data = load('EEG_data.mat');
eeg = data.EEG;
fs = data.fs;
% 设计滤波器
low_freq = 1; % 低通截止频率
high_freq = 50; % 高通截止频率
order = 4; % 滤波器阶数
[b, a] = butter(order, [low_freq high_freq]/(fs/2));
% 滤波
eeg_filtered = filtfilt(b, a, eeg);
% 绘制原始信号和滤波后的信号
t = (:length(eeg)-1)/fs;
figure;
subplot(2,1,1);
plot(t, eeg);
title('原始信号');
xlabel('时间 (s)');
ylabel('电压 (uV)');
subplot(2,1,2);
plot(t, eeg_filtered);
title('滤波后的信号');
xlabel('时间 (s)');
ylabel('电压 (uV)');
相关问题
脑电信号频域分析butterworth滤波器代码
以下是脑电信号频域分析butterworth滤波器代码的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846
void butterworth_filter(double *input, double *output, int n, double fc, int fs)
{
double w = 2 * PI * fc / fs;
double a = tan(w / 2);
double a2 = a * a;
double b = 1 / (1 + sqrt(2) * a + a2);
double b1 = 2 * b;
double b2 = b;
double a1 = 2 * b * (a2 - 1);
double a2 = b * (1 - sqrt(2) * a + a2);
double x1 = , x2 = , y1 = , y2 = ;
for (int i = ; i < n; i++) {
output[i] = b * input[i] + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
x2 = x1;
x1 = input[i];
y2 = y1;
y1 = output[i];
}
}
int main()
{
int n = 100;
double fs = 100;
double fc = 50;
double *input = (double *)malloc(n * sizeof(double));
double *output = (double *)malloc(n * sizeof(double));
for (int i = ; i < n; i++) {
input[i] = sin(2 * PI * i * 10 / fs) + sin(2 * PI * i * 100 / fs);
}
butterworth_filter(input, output, n, fc, fs);
for (int i = ; i < n; i++) {
printf("%f\n", output[i]);
}
free(input);
free(output);
return ;
}
```
matlab提取脑电信号的频域特征做PSD可视化代码
MATLAB是一种强大的数值计算工具,常用于信号处理领域,包括提取脑电图(EEG)信号的频域特性,如功率谱密度(PSD)。以下是基本步骤来提取脑电信号的PSD并生成可视化的Matlab代码示例:
```matlab
% 加载脑电数据 (假设数据文件名为eeg_data.mat)
load('eeg_data.mat');
% 提取单个通道的数据(例如,选择第1个通道)
channel_data = eeg_data(:,1);
% 对数据进行预处理(例如去除直流偏移、滤波等)
if needed
channel_data = detrend(channel_data); % 去除趋势
[b,a] = butter(2, [0.5 30]/50, 'bandpass'); % 设定滤波参数
channel_data = filtfilt(b,a,channel_data);
end
% 计算功率谱密度(PSD)
[psd,freq] = pwelch(channel_data, [], [], [], Fs, 'onesided'); % Fs是采样频率
% 绘制PSD图表
figure;
plot(freq, psd, 'LineWidth', 2);
xlabel('Frequency (Hz)');
ylabel('Power Spectral Density');
title('Single-channel EEG PSD');
grid on;
% 可视化显著的频率成分(如阿尔法、贝塔、伽马带等)
alpha_band = freq(freq >= 8 & freq <= 13); % 假设阿尔法带范围
beta_band = freq(freq >= 13 & freq <= 30); % 假设贝塔带范围
gamma_band = freq(freq >= 30 & freq <= 50); % 假设伽马带范围
hold on;
h_alpha = plot(alpha_band, psd(freq == alpha_band), 'r--', 'LineWidth', 2, 'Marker', 'o');
hold on;
h_beta = plot(beta_band, psd(freq == beta_band), 'g:', 'LineWidth', 2, 'Marker', '*');
hold on;
h_gamma = plot(gamma_band, psd(freq == gamma_band), 'k-.', 'LineWidth', 2, 'Marker', '^');
legend([h_alpha; h_beta; h_gamma], {'Alpha band', 'Beta band', 'Gamma band'}, 'Location', 'Best');
```
请注意,这只是一个基础示例,实际应用中可能需要根据具体需求对数据进行更复杂的预处理,并可能涉及多通道数据的分析。对于初学者,建议先熟悉MATLAB的基本语法和信号处理库(如Signal Processing Toolbox)的功能。如果你遇到问题,可以询问具体的技术细节。
阅读全文