java 绘制频谱图_频谱图是否显示正确的值?

本文探讨了如何使用Java绘制频谱图,并针对一个非平稳信号的频谱表示提出疑问。作者展示了MATLAB代码,创建了一个包含不同频率分量的信号,并尝试通过spectrogram函数分析。在更新部分,作者调整了信号频率和参数,以更好地理解频谱图的显示。然而,遇到了内存问题并在寻求解决方案。
摘要由CSDN通过智能技术生成

我想知道下面发布的 spectrogram 是否是给定非平稳信号的真实表示 .

如果它是真实的表示,我对图中的特定功能有很多疑问......

对于水平轴上的0 - > .25,为什么它会显示最高频率的信号分量?我假设,鉴于第一次持续时间 t1 ,我应该只看到信号 x1 的频率 . 此外,给定第二个持续时间 t2 ,我应该只看到信号 x2 的频率,依此类推 . 但是,这不是我在下面发布的 spectrogram .

你能解释为什么我们在频谱图中看到这些功能吗?

Spectrogram With equations

df7cbf6d-cfa7-4d2a-bdad-12b002c8a286.jpg

Code :

% Time specifications:

Fs = 8000; % samples per second

dt = 1/Fs; % seconds per sample

StopTime = 1; % seconds

t = (0:dt:StopTime-dt); % seconds

t1 = (0:dt:.25);

t2 = (.25:dt:.50);

t3 = (.5:dt:.75);

t4 = (.75:dt:1);

x1 = (10)*sin(2*pi*10*t1);

x2 = (10)*sin(2*pi*20*t2) + x1;

x3 = (10)*sin(2*pi*30*t3) + x2;

x4 = (10)*sin(2*pi*40*t4) + x3;

NFFT = 2 ^ nextpow2(length(t)); % Next power of 2 from length of y

Y = fft(x4, NFFT);

f = Fs / 2 * linspace(0, 1, NFFT/2 + 1);

%{

figure;

plot(f(1:200), 2 * abs( Y( 1:200) ) );

%}

T = 0:.01:1;

spectrogram(x4,10,9,NFFT);

ylabel('Frequency');

axis(get(gcf,'children'), [0, 1, 1, 50]);

Update_1 :当我尝试建议的答案时,我收到了以下内容 .

??? Out of memory. Type HELP MEMORY for your options.

Error in ==> spectrogram at 168

y = y(1:length(f),:);

Error in ==> stft_1 at 36

spectrogram(x,10,9,NFFT);

使用的代码:

% Time specifications:

Fs = 8000; % samples per second

dt = 1/Fs; % seconds per sample

StopTime = 1; % seconds

t = (0:dt:StopTime-dt); % seconds

%get a full-length example of each signal component

x1 = (10)*sin(2*pi*10*t);

x2 = (10)*sin(2*pi*20*t);

x3 = (10)*sin(2*pi*30*t);

x4 = (10)*sin(2*pi*40*t);

%construct a composite signal

x = zeros(size(t));

I = find((t >= t1(1)) & (t <= t1(end)));

x(I) = x1(I);

I = find((t >= t2(1)) & (t <= t2(end)));

x(I) = x2(I);

I = find((t >= t3(1)) & (t <= t3(end)));

x(I) = x3(I);

I = find((t >= t4(1)) & (t <= t4(end)));

x(I) = x4(I);

NFFT = 2 ^ nextpow2(length(t)); % Next power of 2 from length of y

Y = fft(x, NFFT);

f = Fs / 2 * linspace(0, 1, NFFT/2 + 1);

%{

figure;

plot(f(1:200), 2 * abs( Y( 1:200) ) );

%}

T = 0:.01:1;

spectrogram(x,10,9,NFFT);

ylabel('Frequency');

axis(get(gcf,'children'), [0, 1, 1, 50]);

Update_2

% Time specifications:

Fs = 8000; % samples per second

dt = 1/Fs; % seconds per sample

StopTime = 1; % seconds

t = (0:dt:StopTime-dt); % seconds

t1 = ( 0:dt:.25);

t2 = (.25:dt:.50);

t3 = (.5:dt:.75);

t4 = (.75:dt:1);

%get a full-length example of each signal component

x1 = (10)*sin(2*pi*100*t);

x2 = (10)*sin(2*pi*200*t);

x3 = (10)*sin(2*pi*300*t);

x4 = (10)*sin(2*pi*400*t);

%construct a composite signal

x = zeros(size(t));

I = find((t >= t1(1)) & (t <= t1(end)));

x(I) = x1(I);

I = find((t >= t2(1)) & (t <= t2(end)));

x(I) = x2(I);

I = find((t >= t3(1)) & (t <= t3(end)));

x(I) = x3(I);

I = find((t >= t4(1)) & (t <= t4(end)));

x(I) = x4(I);

NFFT = 2 ^ nextpow2(length(t)); % Next power of 2 from length of y

Y = fft(x, NFFT);

f = Fs / 2 * linspace(0, 1, NFFT/2 + 1);

%{

figure;

plot(f(1:200), 2 * abs( Y( 1:200) ) );

%}

T = 0:.001:1;

spectrogram(x,10,9);

ylabel('Frequency');

axis(get(gcf,'children'), [0, 1, 1, 100]);

Dpectrogram_2 :

6ffc6bde-ec90-4081-91ae-70d0ae2f6b68.jpg

使用jdk16编译 import java.awt.Graphics; import java.awt.GridLayout; import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; import javax.swing.GroupLayout; import javax.swing.JFrame; /** * */ /** * @author Administrator * */ public class Musicline extends JFrame implements Runnable { private byte[] audioData = null; private int intBytes = 0; private byte[] ml = new byte[1]; private int[] drawl = null; /** Creates new form Musicline */ public Musicline() { initComponents(); Graphics g; g = this.getGraphics(); } public void paint(Graphics g) { g.clearRect(0, 0, 900, 900); // System.out.print(drawl.length); if (audioData != null) { drawl = new int[audioData.length]; for (int i = 0; i < audioData.length; i++) { ml[0] = audioData[i]; // String s=new String(ml); drawl[i] = Math.abs((int) ml[0]); } System.out.println(drawl[0]); for (int i = 0; i < drawl.length - 1; i++) { g.drawLine(i * this.getWidth() / 256, drawl[i] + 100, (i + 1) * this.getWidth() / 256, drawl[i + 1] + 100); } } } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ public void run() { // TODO Auto-generated method stub while (intBytes != -1) { try { synchronized (this) { this.wait(10); } } catch (InterruptedException ex) { ex.printStackTrace(); } repaint(); } } public void play() { try { AudioInputStream ais = AudioSystem.getAudioInputStream(new File( "F:/perl/key2.wav"));// 获得音频输入流 ais = AudioSystem.getAudioInputStream( AudioFormat.Encoding.PCM_SIGNED, ais); AudioFormat baseFormat = ais.getFormat();// 指定声音流中特定数据安排 System.out.println("baseFormat=" + baseFormat); DataLine.Info info = new DataLine.Info(SourceDataLine.class, baseFormat); System.out.println("info=" + info);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值