• Matlab 信号处理【问答随笔·2】


    1. Matlab 简单插值、拟合方法

    在这里插入图片描述
    答:

    clc,clear,close all;
    x = linspace(-2,2,10);
    y = exp(-x.^2);
    figure(1)
    stem(x,y,"LineWidth",1.5)
    grid on
    title('f(x)')
    %interp1线性插值
    xq = linspace(-2,2,20);
    vq1 = interp1(x,y,xq);
    figure(2)
    plot(x,y,'o',xq,vq1,':.',"LineWidth",1.5);
    xlim([-2 2]);
    grid on
    title('interp1线性插值');
    %polyfit多项式拟合插值
    p = polyfit(x,y,7);
    y1 = polyval(p,x);
    figure(3)
    plot(x,y,'g-o',"LineWidth",1.5)
    hold on
    plot(x,y1,"LineWidth",1.5)
    hold off
    xlim([-2 2]);
    grid on
    title('polyfit多项式拟合插值');
    %spline三次样条插值
    yy = spline(x,y,xq);
    figure(4)
    plot(x,y,'o',xq,yy,"LineWidth",1.5)
    xlim([-2 2]);
    grid on
    title('spline三次样条插值');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    2. 数字信号处理实验——FIR滤波器设计

    问:已知输入信号为混有噪声的信号
    x(t)=sin(250πt)+cos(500πt)+cos(700πt)
    ①画出输入信号时域波形和频谱图,并指出其包含的频率成分(以Hz为单位);
    ②假定输入信号的中间频率成分为噪声信号,请问应如何设计数字滤波器处理该混合信号,给出合理的设计指标并说明理由,画出滤波器的频响特性:要求两种以上的实现方案。
    ③用(2)中设计的滤波器完成滤波并验证设计方案,画出滤波器输出信号时域波形和频谱图。

    答:

    clc,clear,close all;
    t = -0.08:0.0001:0.08;
    n = -100:100;
    L = length(n);
    fs = 1000;
    x0 = sin(2*pi*125.*n/fs);  %时域采样后的信号t=nT=n/fs
    x1 = cos(2*pi*250.*n/fs);
    x2 = cos(2*pi*350.*n/fs);
    x3 = x0 + x1 + x2;
    %时域信号
    figure(1)
    subplot(411)
    plot(t,sin(2*pi*125.*t),"LineWidth",1.5)
    grid on 
    subplot(412)
    plot(t,cos(2*pi*250.*t),"LineWidth",1.5)
    grid on 
    subplot(413)
    plot(t,cos(2*pi*350.*t),"LineWidth",1.5)
    grid on
    subplot(414)
    plot(t,sin(2*pi*125.*t)+cos(2*pi*250.*t)+cos(2*pi*350.*t),"LineWidth",1.5)
    grid on
    %滤波器设计
    wn = 3/5;  %截止频率wn为3/5pi(300Hz),wn = 2pi*f/fs
    N = 60;  %阶数选择
    hn = fir1(N-1,wn,boxcar(N));  %10阶FIR低通滤波器
    figure(2)
    freqz(hn,1);
    figure(3)
    y = fftfilt(hn,x3);  %经过FIR滤波器后得到的信号
    plot(n,y,"LineWidth",1.5)
    grid on
    %频谱分析
    X = fft(x3); %未滤波前的频谱
    p2 = abs(X/L);
    p1 = p2(1:L/2+1);
    p1(2:end-1) = 2*p1(2:end-1);
    Y = fft(y);  %输出信号的fft
    P2 = abs(Y/L);
    P1 = P2(1:L/2+1);
    P1(2:end-1) = 2*P1(2:end-1);
    f = fs*(0:(L/2))/L;
    figure(4)
    subplot(211)
    plot(f,p1,"LineWidth",1.5) 
    title('Single-Sided Amplitude Spectrum of X(t)')
    xlabel('f (Hz)')
    ylabel('|p1(f)|')
    grid on
    subplot(212)
    plot(f,P1,"LineWidth",1.5) 
    title('Single-Sided Amplitude Spectrum of Y(t)')
    xlabel('f (Hz)')
    ylabel('|P1(f)|')
    grid on
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    时域波形:分别为125(2pi*125=250pi)、250和350Hz的余弦信号
    在这里插入图片描述
    截止频率为300Hz的FIR低通滤波器:
    在这里插入图片描述
    滤波前和滤波后的频谱:
    在这里插入图片描述
    滤波后的时域信号:
    在这里插入图片描述

    3. Matlab做的热力图为什么不是顺滑的过渡?——伪彩图函数pcolor()的用法

    问题相关代码:

    clear; clc; close all
    [,,raw ] = xlsread('体育设施热力图数据.xlsx');
    mat = cell2mat(raw(2:end, 2:end));
    imagesc(mat); %生成热图
    c=colorbar;
    colormap hot;
    ylabel(c,'Population Heat');
    caxis([1 11]) %更改右侧颜色条最大最小值
    xticks(1:13) %x轴分成13等分
    xticklabels(raw(1,2:end))
    yticks(1:15)
    yticklabels(raw(2:end,1))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述
    预期效果:
    在这里插入图片描述
    答:能是数据量较少的原因,预期效果应该用了插值;
    而不仅仅是用的imagesc,更像是颜色插值后的伪彩图,建议尝试pcolor()

    clc,clear,close all;
    data=round(rand(1,900)*100);
    data=reshape(data,10,90);
    h=pcolor(data);
    h.FaceColor = 'interp';
    set(h,'LineStyle','none');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    clc,clear,close all;
    data=round(rand(1,900)*100);
    data=reshape(data,10,90);
    h=pcolor(data);
    h.FaceColor = 'interp';
    colormap jet;
    colorbar; 
    set(h,'LineStyle','none');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述
    在这里插入图片描述
    伪彩图函数pcolor()的用法:pcolor官方文档

  • 相关阅读:
    【CNN】SENet——将注意力机制引入通道维度
    Vue.js的单向数据流:让你的应用更清晰、更可控
    机器学习——DBSCAN聚类算法
    【QML】一文入门QML应用程序的性能分析
    Windows下的RabbitMQ安装教程(遇到很多无语的问题,已解决)
    Compose 动画艺术探索之灵动岛
    css让图片的某些区域拉伸,其他部分保持比例,起到类似于安卓中点九.9图的效果
    MySQL 索引的使用和设计
    恒运资本:减肥药概念涨疯了!特斯拉一夜暴涨5800亿市值,汽车股狂飙
    你的第一款开源视频分析框架
  • 原文地址:https://blog.csdn.net/wayne6515/article/details/125629652