【问题标题】:Getting matrix dimension error while modulating audio waveform on matlab在matlab上调制音频波形时出现矩阵尺寸错误
【发布时间】:2018-10-07 13:15:33
【问题描述】:

我收到以下代码的矩阵维度错误。我在下面尝试做的是调制音频波形,但我无法通过我指定的错误。我已经检查了 sample.wav 的长度并相应地调整了时间轴(t),但我一定做错了什么。如果有人可以提供帮助,我将不胜感激。提前致谢!

 function [sm]= modulation(ss,fc,mtype)
ss= audioread('C:\Users\furka\Documents\MATLAB\sample.wav');     %audio waveform to be modulated is loaded.
plot(ss)
length(ss)
t=linspace(0,3e6,3161538);
fc= input('Carrier Frequency=');            %carrier frequency will be determined by the user


mtype= menu('Modulation type?','dsb','dsbsc','ssb','fm');           %modulation type will be determined by the user
%fs=44100;                                   %sampling frequency is determined for common audio waveform.(44.1kHz)
%t= 0:1/fs:(2e-5)-1/fs;

if mtype==1 
    ka= 0.7;
    sm= ss.*(1+ka*cos(2*pi*fc*t));
    plot(t,sm)

    elseif mtype==2                         %if doublesideband suppress carrier is selected the statements below will be carried out.

    y = ss.*cos(2*pi*fc*t);
    plot(y)
    % sm = fftshift(fft(abs(y)));
  %  frequency_axis= (-fs/2):(fs/length(sm)):(fs/2-fs/length(sm));
    %plot(frequency_axis,sm)

    elseif mtype==3
    sm=0.5*[ss.*cos(2*pi*fc*t)-hilbert(ss).*sin(2*pi*fc*t)];
    plot(t,sm)

    elseif mtype==4
     kf=0.7;            %frequency sensitivity.
    sm= cos(2*pi*fc*t+2*pi*kf*int(ss,t,0,t));
    plot(t,sm)
end
end

【问题讨论】:

  • 您能分享sample.wav 或使用我们都有的wav 文件吗?还有什么是确切的错误,它发生在哪里?
  • 这里是sample.wav:ufile.io/m5mtu。每次调制语句都会出错,导致t和sample的维度不匹配。
  • 我自己也上传了代码。 ufile.io/ht0zb 确切的错误是:使用错误。* 矩阵尺寸必须一致。 Assgn_Modulation 错误(第 16 行) sm= ss.*(1+kacos(2*pifct));使用错误。 矩阵尺寸必须一致。 Assgn_Modulation 中的错误(第 21 行)y = ss.*cos(2*pifct);错误使用 .* 矩阵尺寸必须一致。 Assgn_Modulation 错误(第 28 行) sm=0.5*[ss.*cos(2*pifct)-hilbert(ss).*sin(2*pifc t)];我猜他们都是同一个错误造成的。
  • 我会说你需要使用audioread 的两个输出。第一个是声音,第二个是频率。 [ss, Fs] = audioread(...) 然后是声音的长度 total = length(ss) / Fs。然后t = linspace(0, total, length(ss));
  • 我很高兴得到您的帮助,但仍然遇到同样的错误,我已经按照您表达的方式编辑了我的代码。 function [sm]= modulation(ss,fc,mtype) [ss,Fs]= audioread('C:\Users\furka\Documents\MATLAB\sample.wav'); %audio waveform to be modulated is loaded. Fs %sampling frequency is 44.1kHz for audio waveform. total=length(ss)/ Fs t=linspace(0,total,length(ss)); fc= input('Carrier Frequency='); %carrier frequency will be determined by the user.

标签: matlab modulation


【解决方案1】:

我无法访问为声音文件提供的链接,但 3e6 秒的时间长度似乎是一个很长的声音。我想这大约需要 30 天。

使用 MathWorks 提供的 handel.mat 示例我明白了。

load handel.mat
audiowrite('sample.wav', y, Fs);
[ss, Fs] = audioread('sample.wav');

t = linspace(0, length(ss) / Fs, length(ss));
t = t'; % this is important to match ss <-- this is your matrix dimension error I think
fc = 2000;
ka = 0.7
sm = ss .* (1 + ka * cos(2 * pi * fc * t));

尺寸不匹配的错误来自linspace输出为行向量,audioread输出为列向量。转置一个或另一个。就我而言:t = t'

【讨论】:

  • 转置后,调制工作正常。非常感谢 。你一直很有帮助。顺便说一句,声音文件只有 1:11 分钟长。由于草图波形的 x 轴plot(ss),我采用了 3e6。上传到另一个来源vocaroo.com/i/s1ClXAtgG9j1
猜你喜欢
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多