为什么这两个NumPy数组似乎相互关联?

huangapple go评论103阅读模式
英文:

Why this two numpy arrays seem linked?

问题

我正在编写一个用于加密音频信号的代码,使用子带混淆的算法,我使用以下算法来混合它们:

算法图片链接

Matriz是一个排列矩阵(每行和每列都是全零,只有一个1),sb是我存储混淆的np数组,我想要使用for循环对它进行洗牌,我已经在另一个小数组的文件上测试了该算法,它完美运行,即使当我比较sb[:,0]和sb_midi[:,3]时,它们给出了相同的结果(使用密钥12212将0更改为3),但是当我绘制sb和sb_midi时,它们完全相同,我尝试绘制差异,但是再次绘制相同。

这似乎很荒谬,我无法停止想到我可能犯了一个愚蠢的错误,但我已经为此疯狂寻找了很久,我修复了数百个错误,但这真的很奇怪,有什么想法吗?这是一个学士项目的代码,所以有任何帮助都将是极好的,非常感谢。

我尝试将sb_midi混淆,但它总是与sb相同。

编辑:

sb=np.zeros((len(yll),n_sub)) #创建这个矩阵以在每一列中放置一个下采样信号

sb[:,0]=yll
sb[:,1]=ylh
sb[:,2]=yhl
sb[:,3]=yhh
sb_out=np.zeros((len(yll),n_sub))
sb_midi=np.zeros((len(yll),n_sub))
x_out=np.array((n_sub*len(yll),1))

编辑2:

matriz=matriz_permutacion(key,n_sub) #一个自定义函数生成子带数目的排列矩阵
traspuesta=np.transpose(matriz)

plt.figure(4)
plt.plot(20*np.log10(abs(fft(sb))), color = 'red', label = 'x')
plt.title('Sb')
plt.xlabel('时间')
plt.ylabel('振幅')
plt.savefig(r'Downloads\codigos_tfg\figuras\x.png')

i=np.argmax(matriz,axis=1)
#print(i)

for N in range(0, n_sub):
   j=i[N]
   sb_midi[:,j]=sb[:,N]

plt.figure(5)
plt.plot(20*np.log10(abs(fft(sb_midi))), color = 'red', label = 'x')
plt.title('sb_midi')
plt.xlabel('时间')
plt.ylabel('振幅')
plt.savefig(r'Downloads\codigos_tfg\figuras\x.png')
英文:

I am doing a code for cyphering audio signals using subband scrambling, i use this algorithm to mix them:

https://imgur.com/gallery/POuKa17

Matriz is a matrix of permutation (all zeroes except 1 on each row and column), sb is the np array where i store the scrambling, and i wanna shuffle it with the for loop, i have tested that algorithm on another file with small arrays and it worked perfectly, even when i compare sb[:,0] and sb_midi[:,3] they give the same result (with a key of 12212 it changes the 0 to the 3), but when i plot sb and sb_midi they are exactly the same, i tried to plot the difference but, again, it plots the same.

This seems ridiculous and i cant stop thinking that i made a stupid mistake but i am going crazy looking for it, i fixed hundreds of bugs but this is really weird, any idea? this is a code for a bachelor project so it would be great to have any help, thank you so much.

I tried to get sb_midi scrambled but it is always the same as sb

edit:

sb=np.zeros((len(yll),n_sub)) #creamos esta matriz para en cada columna ubicar una señal diezmada

    sb[:,0]=yll
    sb[:,1]=ylh
    sb[:,2]=yhl
    sb[:,3]=yhh
    sb_out=np.zeros((len(yll),n_sub))
    sb_midi=np.zeros((len(yll),n_sub))
    x_out=np.array((n_sub*len(yll),1))

edit 2:

 matriz=matriz_permutacion(key,n_sub) #una función propia genera la matriz de permutación de orden el numero de subbandas
  traspuesta=np.transpose(matriz)

  plt.figure(4)
  plt.plot(20*np.log10(abs(fft(sb))), color = 'red', label = 'x')
  plt.title('Sb')
  plt.xlabel('tiempo')
  plt.ylabel('amplitud')
  plt.savefig(r'Downloads\codigos_tfg\figuras\x.png')


  i=np.argmax(matriz,axis=1)
  #sb_midi.dtype=int
  #sb.dtype=int
  print(i)
 
  for N in range(0, n_sub):
   #[j,i]=np.max(matriz[:N, :]), np.argmax(matriz[:n, N])
   j=i[N]
   sb_midi[:,j]=sb[:,N]


  #sb_midi=sb_midi[:n_sub,:n_sub]

  plt.figure(5)
  plt.plot(20*np.log10(abs(fft(sb_midi))), color = 'red', label = 'x')
  plt.title('sb_midi')
  plt.xlabel('tiempo')
  plt.ylabel('amplitud')
  plt.savefig(r'Downloads\codigos_tfg\figuras\x.png')

答案1

得分: 2

因为:

traspuesta=np.transpose(matriz)

创建了一个对原始缓冲区的视图

import numpy as np
arr = np.array([[1,2,3], [4,5,6]])
transposed = np.transpose(arr)
arr
array([[1, 2, 3],
       [4, 5, 6]])
transposed
array([[1, 4],
       [2, 5],
       [3, 6]])
transposed.flags
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
transposed.base
array([[1, 2, 3],
       [4, 5, 6]])
transposed.base is arr
True

这两个数组对象共享相同的底层缓冲区,transpose 只是简单地切换为 F-连续。

如果你想要一个新的数组,你应该明确地进行复制:

tranposed = np.transpose(arr).copy()

另外,你也可以使用 .T 来对数组进行转置:

transposed = arr.T.copy()
英文:

Because:

traspuesta=np.transpose(matriz)

creates a view over the orignal buffer:

>>> import numpy as np
>>> arr = np.array([[1,2,3], [4,5,6]])
>>> transposed = np.transpose(arr)
>>> arr
array([[1, 2, 3],
       [4, 5, 6]])
>>> transposed
array([[1, 4],
       [2, 5],
       [3, 6]])
>>> transposed.flags
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
>>> transposed.base
array([[1, 2, 3],
       [4, 5, 6]])
>>> transposed.base is arr
True

These two array objects share the same underlying buffer, the transpose one simply is switched to being f-contiguous.

If you want a new array, you should copy it explicitly:

tranposed = np.transpose(arr).copy()

As an aside, you can use .T to transpose an array:

transposed = arr.T.copy()

huangapple
  • 本文由 发表于 2023年8月4日 07:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定